我想问用户多个键值对并存储这些,以便我以后可以使用它们。我尝试了几件事:
columnCombo = {(x for x in input("Enter the Column Name: ")) : (y for y in input("\nEnter the Column Type: "))}
columnCombo = [(x for x in input("Enter the Column Name: ")),(y for y in input("\nEnter the Column Type: "))]
当我打印出来时,我得到:
[<generator object <genexpr> at 0x1019452d0>, <generator object <genexpr> at 0x101945318>]
我真正想要的是要求用户输入列的名称,然后询问该列中包含的数据类型。理想情况下,我可以提取名称(字符串)和关联(类型)
答案 0 :(得分:2)
您可以将这些对象存储在元组中,如下所示:
done = False;
key_value_pairs = []
while not done:
col_name = input("Enter the Column Name: ")
if col_name == "":
done = True
col_type = input("Enter the Column Type: ")
if col_type == "":
done = True
key_value_pairs.append((col_name, col_type))
然后,一旦完成,您可以像这样迭代您的条目:
for key_value_pair in key_value_pairs:
key = key_value_pair[0]
value = key_value_pair[1]
答案 1 :(得分:1)
column = {}
print "when done press ctrl+c"
while True:
try:
col_name = input("Enter the Column Name: ")
col_type = input("Enter the column type: ")
column[col_name] = col_type
except KeyboardInterrupt:break
在上面的代码中,它将提示输入,直到用户按下键盘中断
循环遍历column
字典
for key in column.keys():
col_name = key
col_type = column[key]