[英]python iterating over a list of lists
I have: 我有:
master_list = [['001', '15\n', '963789', '40\n', '741239', '80\n', '985697', '80\n', '854698', '35\n', '965874', '10\n'],
['002', '25\n', '326574', '65\n', '944223', '40\n', '312689', '45\n', '225869', '80\n', '789654', '35\n'],
['003', '10\n', '857963', '50\n', '253698', '40\n', '965478', '50\n', '186458', '40\n', '351296', '40\n'],
['004', '20\n', '675964', '40\n', '612985', '40\n', '653674', '35\n', '957296', '50\n', '852169', '40\n'],
['005', '13', '246912', '40\n', '371956', '40\n', '819736', '40\n', '915745', '50\n', '197548', '40']]
the first two items of the inner list are an id and a pay rate. 内部列表的前两个项目是ID和支付率。 as in master[0][0], master[0][1]
如master [0] [0],master [0] [1]
i then have: 然后我有:
for row in master_list:
emp_id = row.pop(0)# extracts the employee id
hourly_rate = row.pop(0)# extracts the hourly rate
the row
then becomes something like this: ['963789', '40\\n', '741239', '80\\n', '985697', '80\\n', '854698', '35\\n', '965874', '10\\n']
该
row
变成这样: ['963789', '40\\n', '741239', '80\\n', '985697', '80\\n', '854698', '35\\n', '965874', '10\\n']
the first item in this row is a job number and the second item is hours worked. 该行的第一项是工作编号,第二项是工作时间。 the third item is a job number and the fourth item is hours worked etc. until the end of the list...
第三项是工作编号,第四项是工作时间,等等,直到列表结尾...
How would I iterate this list to print something like: 我将如何遍历此列表以打印以下内容:
Job Number: 963789
Employee ID: 001
Hours Worker: 40
Hourly Rate: 15
Job Number: 741239
Employee ID: 001
Hours Worker: 80
Hourly Rate: 15
etc... 等等...
For each row you can access the data like so 对于每一行,您可以像这样访问数据
job_number = row[::2]
hours_worked = row[1::2]
To print, just do something like this 要打印,只需执行以下操作
for row in master_list:
emp_id = row.pop(0)# extracts the employee id
hourly_rate = row.pop(0)# extracts the hourly rate
job_number = row[::2]
hours_worked = row[1::2]
for i in range(len(job_number)):
print "Job Number: {jn}\nEmployee ID: {eid}\nHours Worked: {hrw}\nHourly Rate: {hrr}\n".format(jn=rob_number[i], eid=emp_id, hrw=hours_worked[i], hrr=hourly_rate)
This is one way to go about it (explanations in the code comments): 这是一种解决方法(代码注释中的解释):
titles = ['Employee ID', 'Hourly Rate', 'Job Number', 'Hours Worker'] #assuming these are the only titles you are interested in
master_list = [['001', '15\n', '963789', '40\n', '741239', '80\n', '985697', '80\n', '854698', '35\n', '965874', '10\n'],
['002', '25\n', '326574', '65\n', '944223', '40\n', '312689', '45\n', '225869', '80\n', '789654', '35\n'],
['003', '10\n', '857963', '50\n', '253698', '40\n', '965478', '50\n', '186458', '40\n', '351296', '40\n'],
['004', '20\n', '675964', '40\n', '612985', '40\n', '653674', '35\n', '957296', '50\n', '852169', '40\n'],
['005', '13', '246912', '40\n', '371956', '40\n', '819736', '40\n', '915745', '50\n', '197548', '40']]
new_master_list = [a_list[:4] for a_list in master_list] #since you are only interested in first 4 items in a sub list
for a_list in new_master_list:
for item in a_list:
print('{}: {}'.format(titles[a_list.index(item)], item.strip()))
print() # for the new line after every sub list
This gives: 这给出:
Employee ID: 001
Hourly Rate: 15
Job Number: 963789
Hours Worker: 40
Employee ID: 002
Hourly Rate: 25
Job Number: 326574
Hours Worker: 65
Employee ID: 003
Hourly Rate: 10
Job Number: 857963
Hours Worker: 50
Employee ID: 004
Hourly Rate: 20
Job Number: 675964
Hours Worker: 40
Employee ID: 005
Hourly Rate: 13
Job Number: 246912
Hours Worker: 40
The list titles
is just for printing purposes. 列表
titles
仅用于打印目的。 When iterating over the elements of the inner list ( a_list
, which is the same as row
in yours), the code picks the corresponding element from the titles
list by doing titles[a_list.index(item)]
and prints them together. 当遍历内部列表的元素(
a_list
,与您的row
相同)时,代码通过执行titles[a_list.index(item)]
从titles
列表中选择相应的元素并将它们一起打印。
Documentation on the python string formatting specifier {}
here . 有关python字符串格式说明符
{}
文档,请参见此处 。 The line: 该行:
[a_list[:4] for a_list in master_list]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.