将列表条目组合到字典中

时间:2013-09-07 15:09:50

标签: python list class dictionary python-3.x

我试图将first_name和last_name组合成一个新的字典条目。多少取决于有多少人在商店购买。如果我购买了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)

输出:

        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

2 个答案:

答案 0 :(得分:4)

您正在创建随机名称,其中一些名称将是相同的,因此它们会替换字典中的先前条目(字典键是唯一的)。你必须改变你这样做的方式。例如:

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

但是你也应该得到更大的名字和姓氏池,否则你只能有63个不同的全名。

答案 1 :(得分:1)

字典的问题是字典键必须是唯一的。由于您使用随机选择的名称拼接在一起作为键,因此您很可能会多次生成'Ryan Wright'(例如)。

以下是您的代码正在执行的操作,这会导致您提出“短”计数:

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

第三个分配使用字典troops中的相同插槽,因为密钥是相同的。如果您的代码只是那三行,那么您将拥有一个包含两个条目的字典,而不是您希望的三个条目。通过添加assert语句,您可以在代码中看到这种情况:

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

它不会解决您的问题,但会告诉您钥匙正在发生碰撞。