gpt4 book ai didi

python - 我有一个列表,其中包含列表中的项目,我想为每个列表指定一个名称

转载 作者:太空宇宙 更新时间:2023-11-03 17:55:38 25 4
gpt4 key购买 nike

我有一个列表,其中包含列表中的项目,我想为每个列表命名,但我不知道列表中的项目数量是多少:

lists = [[1.0, 2.0], [3.0, 4.0, 1.0, 2.0]...]

#result:
0 = [1.0, 2.0]
1 = [3.0, 4.0, 1.0, 2.0]
... = []

#I have this script but doesn’t work


number_of_items = len(listas)
names = range(number_of_items)



for x in names:
for i in listas:
x = [i]



#any advice?

最佳答案

您可以将它们全部放入字典中,例如:

lists = [[1.0, 2.0], [3.0, 4.0, 1.0, 2.0], [4,5,6]]


out_dict = {}
for i,l in enumerate(lists):
out_dict[i] = l

print(out_dict)

# which gives
{0: [1.0, 2.0], 1: [3.0, 4.0, 1.0, 2.0], 2: [4, 5, 6]}

但是如果您使用 0,1,2 作为键/名称,则无论如何都像使用原始列表一样。

或者,您可以将变量添加到命名空间,如下所示:

for i,l in enumerate(lists):
locals()['v'+str(i)] = l


print(v0, v1, v2)
#[1.0, 2.0] [3.0, 4.0, 1.0, 2.0] [4, 5, 6]

关于python - 我有一个列表,其中包含列表中的项目,我想为每个列表指定一个名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28491994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com