python迭代列表列表

时间:2014-07-06 04:29:17

标签: python list loops for-loop

我有:

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']]

内部列表的前两项是id和支付率。如在主[0] [0]中,主[0] [1]

然后我有:

for row in master_list:
        emp_id = row.pop(0)# extracts the employee id
        hourly_rate = row.pop(0)# extracts the hourly rate

row然后变成这样: ['963789', '40\n', '741239', '80\n', '985697', '80\n', '854698', '35\n', '965874', '10\n']

此行中的第一项是作业编号,第二项是工作小时数。第三项是工作号,第四项是小时工作等,直到列表末尾......

我如何迭代此列表以打印如下内容:

Job Number: 963789
Employee ID: 001
Hours Worker: 40
Hourly Rate: 15

Job Number: 741239
Employee ID: 001
Hours Worker: 80
Hourly Rate: 15

等...

2 个答案:

答案 0 :(得分:2)

对于每一行,您都可以像这样访问数据

job_number = row[::2]
hours_worked = row[1::2]

要打印,只需执行此类操作

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)

答案 1 :(得分:2)

这是一种解决方法(代码注释中的解释):

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

这给出了:

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

列表titles仅用于打印目的。迭代内部列表的元素(a_list,与您的row相同)时,代码通过titlestitles[a_list.index(item)]列表中选择相应的元素并将它们打印在一起。

有关python字符串格式说明符{} here的文档。这一行:

[a_list[:4] for a_list in master_list]

是列表理解。更多信息here