gpt4 book ai didi

python - 以 3 步 3 步的方式打印字典列表

转载 作者:行者123 更新时间:2023-11-30 23:18:33 26 4
gpt4 key购买 nike

我必须创建一个如下所示的字典列表理解:

[ {0: [1, 2, 3]}, {1: [2, 3, 4]}, {2: [3, 4, 5]} … {100: [101, 102, 103]} ]

用这段代码创建了这个:

a=[{I-1:range(I,i+3) for I in range(1,101)]

现在我被困住了,正在以 3 乘 3 的步长打印列表中的每个元素,即:

        {0: [1, 2, 3]}
{3: [4, 5, 6]}
{6: [7, 8, 9]}

{99: [100, 101, 102]}

最佳答案

您应该能够使用简单的切片:

a[::3]

将为您提供每第三个字典的列表。只需将其放入循环中即可打印:

for dct in a[::3]:
print(dct)

演示:

>>> a=[{I-1:range(I,I+3)} for I in range(1,101)]
>>> for dct in a[::3]:
... print(dct)
...
{0: [1, 2, 3]}
{3: [4, 5, 6]}
{6: [7, 8, 9]}
{9: [10, 11, 12]}
{12: [13, 14, 15]}
{15: [16, 17, 18]}
{18: [19, 20, 21]}
{21: [22, 23, 24]}
{24: [25, 26, 27]}
{27: [28, 29, 30]}
{30: [31, 32, 33]}
{33: [34, 35, 36]}
{36: [37, 38, 39]}
{39: [40, 41, 42]}
{42: [43, 44, 45]}
{45: [46, 47, 48]}
{48: [49, 50, 51]}
{51: [52, 53, 54]}
{54: [55, 56, 57]}
{57: [58, 59, 60]}
{60: [61, 62, 63]}
{63: [64, 65, 66]}
{66: [67, 68, 69]}
{69: [70, 71, 72]}
{72: [73, 74, 75]}
{75: [76, 77, 78]}
{78: [79, 80, 81]}
{81: [82, 83, 84]}
{84: [85, 86, 87]}
{87: [88, 89, 90]}
{90: [91, 92, 93]}
{93: [94, 95, 96]}
{96: [97, 98, 99]}
{99: [100, 101, 102]}

关于python - 以 3 步 3 步的方式打印字典列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26635275/

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