简体   繁体   English

如何在 python 中自动创建字典

[英]How to automate the creation of a dictionnary in python

Hello dear community (and thank you in advance for your time and help).您好,亲爱的社区(提前感谢您的时间和帮助)。

I'am trying to do the simple letter cipher exercise during a python course.我正在尝试在 python 课程中进行简单的字母密码练习。 During the solution (I'am still strying to come up with) I want to creat two dictionnaries one with the alphabet as the keys and numbers as values: exemple alphabet_dict{"a":1,"b":2,"c":3,...... and another with the numbers as keys and the alphabet as values.在解决方案期间(我仍在努力提出)我想创建两个字典,其中一个以字母作为键,数字作为值:例如alphabet_dict{"a":1,"b":2,"c":3,......另一个以数字为键,字母为值。 I want to automate it or at least creat it with code instead of typing it in (it's more pythonic isn't it?) Here is where I'am stuck, here is my code:我想自动化它或者至少用代码创建它而不是输入它(它更像是pythonic,不是吗?)这是我卡住的地方,这是我的代码:

import string
alphabet = string.ascii_lowercase

alpha_dict = {}

for char in alphabet:
   
    alpha_dict[char].append(i for i in range(1.26))

print(alpha_dict)

for the numbers dictionary, the code string.digit gives only up to 9, how to automate the creation of a dictionary up to 26 without typing it.对于数字字典,代码string.digit最多只给出 9,如何在不输入的情况下自动创建最多 26 个字典。

Many many thanks in advance.非常感谢提前。

You can try你可以试试

import string

alphabet = string.ascii_lowercase
alpha_dict = {letter:index+1 for index,letter in enumerate(alphabet)}

You can reverse the order to get the other dictionary您可以颠倒顺序以获取其他字典

use string.ascii_lowercase and convert to list type使用string.ascii_lowercase并转换为列表类型

import string

alphabet = list(string.ascii_lowercase)

alphabet: ascending order字母:升序

alphabet_dict = {alphabet[idx] : idx + 1 for idx in range(len(alphabet))}

alphabet: descending order字母:降序

alphabet_dict2 = {alphabet[idx] : idx + 1 for idx in range(len(alphabet)-1, -1, -1)}

I guess this is the code you are searching for:我想这是您正在搜索的代码:

import string
al_num = {}
num_al = {}
number = 1
for letter in string.ascii_lowercase:
    al_num[letter] = number
    num_al[number] = letter
    number+=1
print(al_num)
print(num_al)

Output: Output:

{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}

{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}

Instead of creating variables, you can append dictionaries as an item of a list.您可以将append字典作为列表项,而不是创建变量。

# Variable to store dictionaries
together = []

# run 10 times
for i in range(10):
    dictionary = {'index': i}
    together.append(dictionary)

print(together)

Then you will get:然后你会得到:

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

Check out the chr and ord functions.查看chrord函数。 They will help you.他们会帮助你。

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

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