简体   繁体   English

将列表条目组合到字典中

[英]Combining list entries into a dictionary

I am trying to get the first_name and last_name to combine together into a new dictionary entry. 我正在尝试获取first_name和last_name一起组合成一个新的词典条目。 How many depends on how many people buy in the shop. 多少取决于在商店购买的人数。 If I buy 50 troops I always receive less than that. 如果我购买50个部队,我得到的总是少于这个。

import random, math
first_name = [ "Emily", "Steve" , "Dave" , "Bob" , "James" , "Jim" , "Jenny" , "Will" , "Ryan" ]
last_name = [ "Wright" , "Kalman" , "Meitzen" , "Cole" , "Robins" , "Harrison" , "Saturn" ]
troops = {}
money = 1000

def shop():
    loop = 0
    global money, first_name, last_name, troops
    while loop == 0 :
        print("""
        Your Money = {}
        (Number , Item , cost)
        0, Quit ,N/A
        1, Troop, 10
        """.format(money))
        shopq = int( input("What do you want to buy"))

        if shopq == 1:
            shopq2 = int( input("How many"))
            if shopq2 > money :
                print(" You cannot by this many")
            else:
                print("You can buy that many")
                money = money - shopq2
                troop_number = 0
                while troop_number < shopq2 :
                    s_name = random.choice(first_name) + " " + random.choice(last_name)
                    troops[s_name] = 100
                    troop_number = troop_number + 1
                print(troops)

            print(" Money = {}".format(money))
        elif shopq == 0:
            break

class dropship:
    def create(self, troops):
        troop_number = 0
        for k in troops :
            troop_number = troop_number + 1
        print("troops = {}".format(troop_number))

shop()
x = dropship()
x.create(troops)

Output: 输出:

        Your Money = 1000
        (Number , Item , cost)
        0, Quit ,N/A
        1, Troop, 10

What do you want to buy1
How many50
You can buy that many
{'Ryan Wright': 100, 'Bob Cole': 100, 'Bob Kalman': 100, 'Will Wright': 100, 'Dave Cole': 100, 'Dave Robins': 100, 'Emily Kalman': 100, 'Jenny Kalman': 100, 'Bob Harrison': 100, 'Emily Wright': 100, 'Will Cole': 100, 'Jim Wright': 100, 'Dave Kalman': 100, 'Dave Wright': 100, 'Bob Meitzen': 100, 'Jenny Wright': 100, 'Jenny Harrison': 100, 'Dave Saturn': 100, 'James Robins': 100, 'Bob Robins': 100, 'Dave Meitzen': 100, 'Steve Wright': 100, 'Bob Wright': 100, 'Steve Kalman': 100, 'Ryan Harrison': 100, 'Jim Saturn': 100, 'Steve Robins': 100, 'Ryan Cole': 100, 'Jim Meitzen': 100, 'James Cole': 100, 'Emily Cole': 100, 'Ryan Saturn': 100, 'Steve Harrison': 100}
 Money = 950

        Your Money = 950
        (Number , Item , cost)
        0, Quit ,N/A
        1, Troop, 10

What do you want to buy0
troops = 33

You are creating random names and some of them will be the same, by chance, so they replace the previous entries in the dictionary (dictionary keys are unique). 您正在创建随机名称,其中一些名称偶然会相同,因此它们会替换字典中的先前条目(字典键是唯一的)。 You'll have to change the way you do that. 您将必须更改操作方式。 For instance: 例如:

import random
import itertools
random.sample(list(itertools.product(first_name, last_name)), 50)

But you should also get much larger pools of first and last names, otherwise you can only have 63 different full names. 但是,您还应该获得更大的名字池和姓氏池,否则只能有63个不同的全名。

The problem with your dictionary is that dictionary keys must be unique. 字典的问题在于字典键必须唯一。 Since you are using randomly chosen names spliced together as keys, it is very likely that you will generate 'Ryan Wright' (for example) more than once. 由于您使用拼接在一起的随机选择的名称作为键,因此很可能会多次生成'Ryan Wright' (例如)。

Here is what your code is doing that is causing you to come up with a "short" count: 这是您的代码正在执行的操作,这导致您得出“短”计数:

troops['Ryan Wright'] = 100
troops['Bob Cole'] = 100
troops['Ryan Wright'] = 100

The third assignment used the same slot in the dictionary troops because the key is the same. 第三个任务在字典troops使用了相同的插槽,因为密钥是相同的。 If your code was just those three lines you'd have a dictionary with two entries in it, not the three that you'd hope for. 如果您的代码只是这三行,那么您将拥有一个包含两个条目的字典,而不是您希望的三个条目。 You can see this happen in your code by adding the assert statement: 您可以通过添加assert语句在代码中看到这种情况:

s_name = random.choice(first_name) + " " + random.choice(last_name)
assert s_name not in troops
troops[s_name] = 100

It won't fix your problem, but it will show you that your keys are colliding. 它不会解决您的问题,但会告诉您密钥正在碰撞。

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

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