我用以下代码制作了一本字典。我想添加一个字符串"英语"所有值,但因为它不接受值中的整数。
key = ["I", "you", "we", "us", "they", "their"]
value = list(range(len(key)))
dictionary = dict(zip(key,value))
print(dictionary)
输出:
{'they': 4, 'I': 0, 'you': 1, 'we': 2, 'us': 3, 'their': 5}
我想要关注输出:
output = {'they': 'English 4', 'I': 'English 0', 'you': 'English 1', 'we': 'English 2', 'us': 'English 3', 'their': 'English 5'}
答案 0 :(得分:1)
您可以使用列表理解。变化
{ Component }
答案 1 :(得分:1)
你的意思是每个值都有2个不同的字段(一个是索引,一个是语言)。要做到这一点,您只需将 /.auth/login/<provider>?post_login_redirect_url=YOUR_URL
转换为元组列表,而不是单个值。
因此value
应包含value
您可以使用((0, "English"), (1, "English"), ... (len(key), "English"))
轻松完成此操作:
enumerate
输出:
value = enumerate(["English"] * len(key))
(您可能已经意识到:{'their': (5, 'English'), 'you': (1, 'English'), 'us': (3, 'English'), 'I': (0, 'English'), 'they': (4, 'English'), 'we': (2, 'English')}
会返回附加了索引的enumerate(a)
的每个项目,即a