我到处寻找答案,但找不到。我有一个包含这些键/值的数据文件:
oolong:8580.0:7201.25:8900.0
earl grey:10225.25:9025.0:9505.0
green:6700.1:5012.45:6011.0
mint:9285.15:8276.1:8705.0
jasmine:7901.25:4267.0:7056.5
数据如下-茶_名称:store1\u Sales:存储2_销售:store3\u销售在
我需要能够显示这个输出:
^{2}$我知道我可以使用
with open('tea.txt') as f:
teas = f.read().splitlines()
我不知道如何将一个列表转换为一个单键具有多个值的dict。感谢任何帮助。在
编辑:我现在知道如何获取列表并转换成字典。谢谢各位!在
获取dict的最简单方法:
解决这个问题有很多不同的方法。我来教你一种读茶叶的方法。在
如果需要每个列表的数值,可以将它们映射到适当的数据类型。下面是两个示例,说明如何更改上面的最后一行以获得数字列表/数组。在
您可以使用简单的内置函数来执行此操作:
或者,您可以使用
numpy
数组最终的字典如下所示:
您可以使用
pandas
轻松完成此操作:结果如下:
^{pr2}$编辑:要从这个
pandas.DataFrame
对象获取dict
,只需执行以下操作:Edit2:忽略
pandas
,您可以像这样在基本Python中完成您想要的等效地,使用
dict
理解:最后,要想在最后得到你的累计销售额:
结果:
相关问题 更多 >
编程相关推荐