简体   繁体   English

提取列表的每个列表

[英]Extracting each list of a lists

I have the following list of lists: 我有以下列表清单:

list_sample = [[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [0.021, 0.021, 0.021, 0.021, 0.021, 0.021, 0.021], [0.022, 0.022, 0.022, 0.022, 0.022, 0.022, 0.022],...]

I'd like to extract each list and attribute it to another list such as: 我想提取每个列表并将其归因于另一个列表,例如:

for i,line in enumerate(list_sample):
    if  i==0:
        Z_level=line
    if i==1:
        XD1=line
    if i==2:
        XD2=line
    .....

Is there a way to make this more efficiently? 有没有办法让这个更有效率? Thanks 谢谢

Or (complementing RobEarl) answer, 或者(补充RobEarl)回答,

Z_level, XD1, XD2, ... = list_sample

However, if you don't know how big is your list_sample you need to look for how to dynamically set variables in python. 但是,如果您不知道list_sample有多大,则需要查找如何在python中动态设置变量。 You can check some of that here 你可以在这里查看一些

If I read you correctly, you want to assign the first sub-list to Z_level, the second to XD1... 如果我读了你,你想把第一个子列表分配给Z_level,第二个子列表分配给XD1 ......

Z_level, XD1, XD2 ... = list_sample

If you only want to assign 3 things: 如果你只想分配3件事:

Z_level, XD1, XD2 = list_sample[:3]

No need for a loop: 不需要循环:

Z_level = list_sample[0]
XD1 = list_sample[1]
XD2 = list_sample[2]
...

To infinity, try this ... 要无限,试试这个......

for i,line in enumerate(list_sample):
    if i == 0:
        vars()["Z_level"] = line
        continue
    vars()["XD"+str(i)] = line

This is just for fun, but you should probably not use this, read the comments by @defuz and @DSM as to why. 这只是为了好玩,但您可能应该使用它,请阅读@defuz和@DSM的评论。

This is probably a better solution : 这可能是一个更好的解决方案:

lists = {}
for i,line in enumerate(list_sample):
    if i == 0:
        lists["Z_level"] = line
        continue
    lists["XD"+str(i)] = line

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

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