amity = 0
erudite = 2
abnegation = 50
dauntless = 10
lista = [amity, erudite, abnegation, dauntless]
dictonary = {} # initialize dictionary
dictionary[amity] = 'amity'# You're mapping the value 0 to the string amity, not the variable amity in this case.
dictionary[abnegation] = 'abnegation'
dictionary[erudite] = 'erudite'
dictionary[dauntless] = 'dauntless'
print(dictionary) # prints all key, value pairs in the dictionary
print(dictionary[0]) # outputs amity.
for item in sorted(lista):
print(dictionary[x]) # prints values of dictionary in an ordered manner.
Python有一个名为
dictionary
的内置数据类型,用于映射键、值对。这正是你在问题中所要求的,把一个value
附加到一个特定的key
。你知道吗你可以多读一些关于词典的内容。你知道吗
我认为您应该创建一个字典,并将变量名作为字符串映射到它们的每个整数值,如下所示:
定义名称和数字之间的映射:
然后排序:
要将结果保留为映射/字典,请调用排序列表上的^{} :
相关问题 更多 >
编程相关推荐