繁体   English   中英

如何将Python中的数据列表转换为每个项目都有一个键的字典

[英]How to convert a list of data in Python to a Dictionary where each item has a key

Google={}
Google["Price"]=[317.68,396.05,451.48,428.03,516.26,604.83,520.63,573.48,536.51,542.84,533.85,660.87,728.9]

我有一个字典“谷歌”,其中键值显示谷歌的36个值。 有没有办法给每个条目一个单独的密钥(其中317.68是1,396.05是2,等等)?

dict(enumerate(google_price_data, start=1))

只需使用enumerate来帮助你的密钥生成任务,并for通过列表中的每个项目进行循环。

干得好:

google_dict = dict()
google_price_data = [317.68,396.05,451.48,428.03,516.26,604.83,520.63,573.48,536.51,542.84,533.85,660.87,728.9]

for i, item in enumerate(google_price_data, start=1):
    google_dict[i] = item

print google_dict

输出:

{
    1: 317.68,
    2: 396.05,
    3: 451.48,
    4: 428.03,
    5: 516.26,
    6: 604.83,
    7: 520.63,
    8: 573.48,
    9: 536.51,
    10: 542.84,
    11: 533.85,
    12: 660.87,
    13: 728.9
}

暂无
暂无

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

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