简体   繁体   English

将用户输入添加到字典

[英]Adding user input to a dictionary

I've been attempting this for the last couple of hours and I just can't get the code right. 在过去的几个小时中,我一直在尝试这样做,但是我无法正确获取代码。 I want to read in user input (eg 'red', 'blue', 'green', 'red') and spit out the colour and the count of that colour in a dictionary. 我想读入用户输入(例如“红色”,“蓝色”,“绿色”,“红色”),然后在字典中吐出颜色和该颜色的计数。

Here is my code - I know something is definitely not right with it especially in setting up the dictionary (it also has a while loop to continually ask for input until a blank is entered by the user) 这是我的代码-我知道某些东西绝对不合适,尤其是在设置字典时(它还有一个while循环,可以不断要求输入,直到用户输入空白为止)

INPUT: 'red', 'blue', 'green', 'red' 输入:“红色”,“蓝色”,“绿色”,“红色”

dict = {}
car_colours = input("Car: ")
frequency = 0
  while car_colours != '':
  dict['frequency'] = car_colours.count(car_colours)
  dict['colours'] = car_colours
  frequency = frequency + 1
  car_colours = input("Car: ")
print(dict)

I also assume I need a for loop to get the desired output below? 我还假设我需要for循环才能在下面获得所需的输出?

DESIRED OUTPUT 期望的输出

Cars that are red: 2
Cars that are blue: 1

I'm not actually even sure I even need this code in what I have above: 我什至不确定我在上面的代码中是否还需要以下代码:

frequency = 0
frequency = frequency + 1

Thanks for any help! 谢谢你的帮助!

There are few issues in your dictionary setup as you suspected. 您怀疑词典设置中存在的问题很少。

'dict' is a keyword in python, I suggest avoiding that as variable name. 'dict'是python中的关键字,我建议避免将其作为变量名。 Python dictionaries are not ordered usually. Python字典通常不排序。 Instead of having frequency and color name separately, save the color name as key and count as value. 不要将频率名称和颜色名称分开保存,而是将颜色名称另存为键并计数为值。

Here is the code with the above changes : 这是经过上述更改的代码:

d = {}
count = 0
car_colours = raw_input("Car: ")
while car_colours != '':
    if d.has_key(car_colours):
        d[car_colours] = d[car_colours] + 1
    else:
        d[car_colours] = 1
    count = count + 1
    car_colours = raw_input("Car: ")

for k,v in d.iteritems():
    print 'Cars that are ' + k + ": " + str(v)

Here is sample test : 这是示例测试: 在此处输入图片说明

Why not use a more suitable data structure than dict ? 为什么不使用比dict更合适的数据结构?

from collections import defaultdict

print "Enter car colours and ^C when done..."
try:
    car_count = defaultdict(int)
    while True:
        car_colour = raw_input("Car colour: ")
        car_count[car_colour] += 1
except KeyboardInterrupt:
    print
    print "Done with input, now the result"
    print

for c in car_count:
    print "Cars that are %s: %d" % (c, car_count[c])

Result will be: 结果将是:

$ python dd.py 
Enter car colours and ^C when done...
Car colour: red
Car colour: red
Car colour: green
Car colour: blue
Car colour: ^C
Done with input, now the result

Cars that are blue: 1
Cars that are green: 1
Cars that are red: 2
$

Note: Even when the int in car_count = defaultdict(int) is not formatted properly, it is a data type. 注意:即使当intcar_count = defaultdict(int)格式不正确,它是一种数据类型。 The defaultdict extends dict such that each index that has not been accessed before automatically gets assigned the initial value of that type. defaultdict扩展了dict ,以便在自动分配该类型的初始值之前未访问的每个索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM