gpt4 book ai didi

python - 遍历字典并在表中返回结果

转载 作者:行者123 更新时间:2023-11-28 20:28:41 25 4
gpt4 key购买 nike

我正在考虑将字典的内容打印到表格中,字典的定义如下:

d = {"date": tuple(date),"open":tuple(open),"close":tuple(close),"min":tuple(min),"max":tuple(max),"gain":tuple(gain),"loss":tuple(loss),"avg_gain":tuple(avg_gain),"avg_loss":tuple(avg_loss)}

我想遍历它以在 shell 中逐行打印,第一行将包含键,接下来的行是元组(日期)、元组(打开)等的内容 ...

最佳答案

如何将键连接到元组的前面,然后使用 zip(*) 转置结果

>>> d={"A":(1.0,2.0,3.0), "B":(4.0,5.0,6.0), "C":(7.0,8.0,9.0)}
>>> for row in zip(*([k]+map(str,v) for k,v in sorted(d.items()))):
... print "\t".join(row)
...
A B C
1.0 4.0 7.0
2.0 5.0 8.0
3.0 6.0 9.0

关于python - 遍历字典并在表中返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4426025/

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