gpt4 book ai didi

python - 如何将 2 个输出一个作为键,另一个作为值存储在字典中

转载 作者:行者123 更新时间:2023-11-30 09:30:35 25 4
gpt4 key购买 nike

如何在Python中将2个输出一个作为键,另一个作为值存储在字典中,键和值是否分别在输出中具有相应的索引

我做了什么:

olabel = []
ldate = []
label = olabel.append(output)
date = ldate.append(ld)
outputList = {label[i]:date[i] for i in range(10)}

我的 K 最近邻居的 2 个输出将是输出和 ld。例如,我的 K 最近邻居的前 2 个输出是:

output = 'Light1on'
ld = '2019-10-28 09:59:00'

输出应该是输出列表,其中标签和日期将被更新到相应的列表,该列表将被制作成字典,其核心索引从列表中形成到字典中:

label=['Light1on']
date =['2019-10-28 09:59:00']
outputList = [Light1on:2019-10-28 09:59:00]

更新一次:

label=['Light1on','Light2on']
date =['2019-10-28 09:59:00','2019-10-28 10:59:00']
outputList = [Light1on:2019-10-28 09:59:00, Light2on:2019-10-28 10:59:00]

最佳答案

zip() 可以用来实现你想要的。

label=["something","anything","nothing"]
date=['31st feb','32nd dec','1st jan']
outputList={i:j for i,j in zip(label,date)}
print(outputList)

输出:

{'something': '31st feb', 'anything': '32nd dec', 'nothing': '1st jan'}

代码中的错误

label = olabel.append(output)
date = ldate.append(ld)

无论您附加多少个元素,label 都是None,因为 append() 不会返回任何内容,它会就地修改列表。

如果您想将 olabel 的值复制到 label,那么您可以这样做。

olabel.append(output)
label=olabel[:]

关于python - 如何将 2 个输出一个作为键,另一个作为值存储在字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59527348/

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