简体   繁体   English

在python中解压缩列表

[英]unpacking a list in python

Somewhere I saw a piece of code where you can unpack a list in a for loop. 我在某处看到了一段代码,您可以在其中解压缩for循环中的列表。 Let's say that I have a list: 假设我有一个列表:

row = ['001', '15\n', '963789', '40\n', '741239', '80\n', '985697', '80\n', '854698', '35\n', '965874', '10\n']

what would be the for loop to unpack the list. 解列表的for循环是什么。 I saw something similar to: 我看到了类似的东西:

for emp_id,pay_rate,job1,hours_worked1,job2,hours_worked2,job3,hours_worked3,job4,hours_worked4,job5,hours_worked5 in row:

what's the correct syntax to unpack this list? 解压缩此列表的正确语法是什么?

That only really works if you have a nested structure. 仅当您具有嵌套结构时,这才真正起作用。 For example: 例如:

l = [(1,2,3), (4,5,6), (7,8,9)]

for a,b,c in l:
    print a,b,c

[OUTPUT]
1 2 3
4 5 6
7 8 9

If you have a flat list like this: 如果您有这样的固定清单:

l2 = [1,2,3,4,5]

You can do: 你可以做:

one, two, three, four, five = l2

简单分配:

emp_id,pay_rate,job1 = ['001', '15\n', '963789']

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

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