[英]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
谢谢
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.