按字母顺序从小到大对两个简单列表进行排序,然后将它们合并到字典中
Sort two simple lists alphabetically and from least to greatest, then merge them into a dictionary
我有这些列表:
aList = ['a','d','n','e','z']
numList=[1,14,5,26,4]
输出应该是:
aDict = {'a':1, 'd':4, 'e':5, 'n':14, 'z':26}
我的代码是:
a = aList.sort()
b = bList.sort()
#merge both into a dict
尝试使用字典理解:
aDict = {key:value for key, value in zip(sorted(aList), sorted(numList))}
print(aDict)
输出:
{'a': 1, 'd': 4, 'e': 5, 'n': 14, 'z': 26}
鉴于:
aList = ['a','d','n','e','z'] numList=[1,14,5,26,4]
首先对列表进行排序:aList.sort(),numList.sort()
然后压缩它们:
aDict = dict(zip(aList,numList))
我有这些列表:
aList = ['a','d','n','e','z']
numList=[1,14,5,26,4]
输出应该是:
aDict = {'a':1, 'd':4, 'e':5, 'n':14, 'z':26}
我的代码是:
a = aList.sort()
b = bList.sort()
#merge both into a dict
尝试使用字典理解:
aDict = {key:value for key, value in zip(sorted(aList), sorted(numList))}
print(aDict)
输出:
{'a': 1, 'd': 4, 'e': 5, 'n': 14, 'z': 26}
鉴于:
aList = ['a','d','n','e','z'] numList=[1,14,5,26,4]
首先对列表进行排序:aList.sort(),numList.sort()
然后压缩它们:
aDict = dict(zip(aList,numList))