简体   繁体   English

用两个不同的变量填充二维列表

[英]Populating a 2D list with two different variables

I'm trying to created a 2D list, with 2 positions in each list but with two different variables.我正在尝试创建一个 2D 列表,每个列表中有 2 个位置,但有两个不同的变量。

This is for a school project, the name is meant to come first and then a homework mark which I would like to insert later.这是一个学校项目,名字应该放在第一位,然后是我想稍后插入的作业标记。 This is my code so far:到目前为止,这是我的代码:

student_group_list = ['BERRY-SMITH Finlay', 'EDWARDS Elizabeth', 'GREGORY Isabella', 'Hendley-Jones Annabel', 'LIS Natalia', 'MANNING Sophie', 'RATCLIFFE George', 'BELLAMY Jacob', 'BOUGHTON Grace', 'GARNETT Archie', 'Maruta Kudzai', 'DAVELIS Alexander', 'DENMAN David', 'JENKIN Jonathan', 'LISTER Thomas', 'CLOWES Edward', 'OGG Holly', 'PUGH Billy']

hwklist = [[student_group_list[i] for j in range(2)] for i in range(len(student_group_list))]
print (hwklist)

I'm want it to output我想要它到 output

[['BERRY-SMITH Finlay', ''], ['EDWARDS Elizabeth', '']...

but right now I'm getting但现在我得到

[['BERRY-SMITH Finlay', 'BERRY-SMITH Finlay'], ['EDWARDS Elizabeth', 'EDWARDS Elizabeth']...

I want to know if there is any way that I can insert variable 1 into the index 0, and variable 2 into the index 1 when the 2D list is created.我想知道是否有任何方法可以在创建 2D 列表时将变量 1 插入索引 0,并将变量 2 插入索引 1。 Thank you for any help.感谢您的任何帮助。

You can do it in the same way you declare the student_group_list , manually:您可以按照与声明student_group_list相同的方式手动执行此操作:

result = [[student, ''] for student in student_group_list]
print(result)

Output Output

[['BERRY-SMITH Finlay', ''], ['EDWARDS Elizabeth', ''], ['GREGORY Isabella', ''], ['Hendley-Jones Annabel', ''], ['LIS Natalia', ''], ['MANNING Sophie', ''], ['RATCLIFFE George', ''], ['BELLAMY Jacob', ''], ['BOUGHTON Grace', ''], ['GARNETT Archie', ''], ['Maruta Kudzai', ''], ['DAVELIS Alexander', ''], ['DENMAN David', ''], ['JENKIN Jonathan', ''], ['LISTER Thomas', ''], ['CLOWES Edward', ''], ['OGG Holly', ''], ['PUGH Billy', '']]

But if the names of the students are unique, I suggest you use a dictionary instead of a nested list.但是如果学生的名字是唯一的,我建议你使用字典而不是嵌套列表。 With the names as keys and the marks as values.以名称为键,标记为值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM