我想要将列表中的项目(例如[1,2,3,4,5,6,7,8,9])转换为另一个列表中的项目(例如这些数字的名称)。
此外,我希望能够进行翻译,以便当用户输入‘1’时,打印'1',类似于'2',等等。
以下是我到目前为止拥有的代码:
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
'eight', 'nine']
myDict = dict(zip(numbers, names))发布于 2011-11-28 12:08:56
使用input函数将允许您接受来自用户的输入并执行查找(如果我理解您所问的内容):
>>> mydict = {'nom':'singe', 'poids':70, 'taille':1.75}
>>> myvar = input()
nom
>>> print(mydict[myvar])
singe发布于 2011-11-28 13:10:33
它不会打印"nom piods“,因为您没有"nom poids”的密钥。
这将完成您想要的操作:
mydict = {'nom':'x', 'poids':'y','foo':'bar'}
print mydict['nom'],mydict['poids']如果你总是使用数字,而不是单词,你可以这样做:
words = ['zero','one','two','three','four','five']
print words[0] # This will print 'zero'
print words[1] # This will print 'one'
print words['1'] # This will not work, you need to use 1 and not '1'https://stackoverflow.com/questions/8291359
复制相似问题