在python中使用for循环生成字典

2024-06-02 00:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python创建一个包含40个键/值对的字典。他们的钥匙应该是a1,a2。。。a10、b1、b2。。。b10,c1,c2。。。c10、d1、d2。。。d10。我有一个for循环,它接受两个变量,并对它们进行修改,使它们在每次循环后都发生变化。它循环40次,为两个变量中的每一个创建40个不同的值

在字典中,键a1的值应该是第一个循环中x和y的值。键a2的值应为第二个循环中x和y的值。这将继续到键d10,该键在第40个循环中的值应为x和y

我该怎么做?我想我可以使用for循环,但是由于我很难理解for循环,我不知道我应该做什么

这是我的密码:

x = 200
y = 202

for i in range(40):
    if y != 202 + 9 * 84:
        y += 84
    else:
        x += 100
        y = y - 9 * 84

我想要一个 看起来像这样的字典:

dictionary = {'a1':(200, 202), 'a2':(# the value of x and y after looping a second time, here it will be 200, 286)}


Tags: a2for字典a1b2a10b1d1
3条回答
letters = "abcd"
dictionary = {}

x = 200
y = 202

for i in range(40):
    key = letters[int(i / 10)] + str(i % 10 + 1)
    dictionary[key] = x, y

    if y != 202 + 9 * 84:
        y += 84
    else:
        x += 100
        y = y - 9 * 84

这是一个优雅的解决方案,它可以跟踪字母和数字,并使用fstring将它们格式化为键

x = 200
y = 202

# Create the dict first
res = {}

# Assign the letter to begin with
key_letter = 'a'

for i in range(0, 40):
    if i != 0 and i % 10 == 0:
        # Increment the letter since 10th element reached
        key_letter = chr(ord(key_letter) + 1)
    # Format the letter with the number
    res[f'{key_letter}{i % 10 + 1}'] = (x, y)
    if y != 202 + 9 * 84:
        y += 84
    else:
        x += 100
        y = y - 9 * 84
print(res)

输出:

{'a1': (200, 202), 'a2': (200, 286), 'a3': (200, 370), 'a4': (200, 454), 'a5': (200, 538), 'a6': (200, 622), 'a7': (200, 706), 'a8': (200, 790), 'a9': (200, 874), 'a10': (200, 958), 'b1': (300, 202), 'b2': (300, 286), 'b3': (300, 370), 'b4': (300, 454), 'b5': (300, 538), 'b6': (300, 622), 'b7': (300, 706), 'b8': (300, 790), 'b9': (300, 874), 'b10': (300, 958), 'c1': (400, 202), 'c2': (400, 286), 'c3': (400, 370), 'c4': (400, 454), 'c5': (400, 538), 'c6': (400, 622), 'c7': (400, 706), 'c8': (400, 790), 'c9': (400, 874), 'c10': (400, 958), 'd1': (500, 202), 'd2': (500, 286), 'd3': (500, 370), 'd4': (500, 454), 'd5': (500, 538), 'd6': (500, 622), 'd7': (500, 706), 'd8': (500, 790), 'd9': (500, 874), 'd10': (500, 958)}

这样,您就不必维护单独的字符串来跟踪所有字母

下面仅基于40次迭代来自a-dx1-10,所以我去掉了for循环

x = 200
y = 202
your_dict = {}

for j in ["a","b","c","d"]:
    for k in range(1,11):
        key = j + str(k)
        your_dict[key] = (x,y)
        if y != 202 + 9 * 84:
            y += 84
        else:
            x += 100
            y = y - 9 * 84

结果:

{'a1': (200, 202), 'a2': (200, 286), 'a3': (200, 370), 'a4': (200, 454), 'a5': (200, 538), 'a6': (200, 622), 'a7': (200, 706), 'a8': (200, 790), 'a9': (200, 874), 'a10': (200, 958), 'b1': (300, 202), 'b2': (300, 286), 'b3': (300, 370), 'b4': (300, 454), 'b5': (300, 538), 'b6': (300, 622), 'b7': (300, 706), 'b8': (300, 790), 'b9': (300, 874), 'b10': (300, 958), 'c1': (400, 202), 'c2': (400, 286), 'c3': (400, 370), 'c4': (400, 454), 'c5': (400, 538), 'c6': (400, 622), 'c7': (400, 706), 'c8': (400, 790), 'c9': (400, 874), 'c10': (400, 958), 'd1': (500, 202), 'd2': (500, 286), 'd3': (500, 370), 'd4': (500, 454), 'd5': (500, 538), 'd6': (500, 622), 'd7': (500, 706), 'd8': (500, 790), 'd9': (500, 874), 'd10': (500, 958)}

相关问题 更多 >