在Python中使用元组作为键

时间:2017-09-16 00:41:57

标签: python python-3.x

我对编程非常陌生,所以请原谅我,如果有什么不合理或者我说错了话。我有一个关于使用元组作为字典键的问题。

首先,我让用户输入一个数字,

num = input("Enter a number here: ")

然后,我将这个数字值转换为元组:

numTup = tuple(num)

接下来,我创建一个将数字键连接到单词值的字典:

numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}

最后,我希望它打印与元组中的键对应的字典值。我很确定这是我弄错了。

print(numWords[numTup])

基本上我试图用这个程序做的是让每个用户输入数字作为一个单词,即。 456将变为"四五六"。

完整(不正确)的脚本:

num = input("Enter a number here: ")
numTup = tuple(num)
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}
print(numWords[numTup])

4 个答案:

答案 0 :(得分:3)

在这种情况下,tuple不是必需的,因为您的字典将处理将键分配给关联的字符串。

num = input("Enter a number here: ")

numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}
for n in num:
    print(numWords[n], end=' ')

演示:

Enter a number here: 456
Four Five Six

答案 1 :(得分:0)

将dict键的type与使用tuple(num)转换为元组后的输入之间存在不匹配。 您可以跳过转换为元组的部分:

num = input("Enter number here: ")
num = input("Enter a number here: ")
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}
print(numWords[num])

OR

索引到元组并通过选择第0个元素来访问元素:

num = input("Enter number here: ")
num = input("Enter a number here: ")
numTup = tuple(num)
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}
print(numWords[numTup[0]])

注意:确保密钥的数据类型和用于访问dict项目的变量是相同的。您可以使用type()命令检查变量的数据类型。

答案 2 :(得分:0)

'''
You can print the number in words given irs numeric
version if by defining your dictionary with
the following format:
    dict{'number': 'word'}

Then, calling the dictionary as follows:
    dict['number']
'''

# Number (keys) and words (values) dictionary
numWords = {'1': "One", '2': "Two", 
'3': "Three", '4': "Four", 
'5': "Five", '6': "Six", 
'7': "Seven", '8': "Eight", 
'9': "Nine", '10': "Ten"}

# Function that prints the number in words
def print_values():
    for k,v in numWords.items():
        print(v)

'''
Alternatively, if you want to print a value using
its key as an argument, you can use the following
funciton:
'''

# Interactive function to print number in words
# given the number
def print_values2():
    key = input("What number would you like to print? ")
    print(numWords[key])

'''
P.s. if you want to add a number to your dictionary
you can use the following function
'''

# Function to modify numWords
def add_number():

    # Specify the number you want to add
    numeric = input("Type in the number: ")

    # Input the number in words
    word = input("Write the number in words: ")

    # Assign number and word to dictionary
    numWords[numeric] = word

    # Return modified dictionary
    return numWords

答案 3 :(得分:-1)

目前尚不清楚为什么要将数字转换为元组,但是如果你想要这个,你就是以错误的方式做的。当你想从int创建一个元组时,你应该执行以下操作:

numTup = (num)

您的完整代码应如下所示:

num = input("Enter a number here: ")
numTup = (num)
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}
print(numWords[numTup])