0

我似乎无法打印列表中的列表。我知道当我对键和值重复 for 循环时会出错。我想知道是否有一种方法可以打印每个排的每个部队名称。

import random, math
first_name = ['John', 'Clay', 'Gordy', 'Erv', 'Sebastian', 'Tilly', 'Jesse', 'Alban', 'Oliver', 'Samuel', 'Joseph', 'Gregory', 'Alair', 'Gilbert', 'Nigel', 'Gibson', 'Oliver', 'Ralph', 'Rufus', 'Garson', 'Ferrol', 'Miles', 'Chilton', 'Charles', 'Gordon', 'Edward', 'Gerald', 'Shel', 'Dean', 'Noah', 'Herbert', 'Humphrey', 'Hanley', 'Ruben', 'Gibson', 'Jonathan', 'Fisk', 'Harold', 'Cristian', 'Andy', 'Kyne', 'Garson', 'Jackson', 'Maitland', 'George', 'Ford', 'Raleigh', 'Fox', 'Forbes', 'Yeardleigh', 'Gordon', 'Francis', 'Jett', 'Fairfax', 'Ford', 'Haines', 'Benjamin', 'Samuel', 'Alban', 'Chip', 'Eric', 'Alban', 'Charles', 'Sherman', 'Harrison', 'Malcolm', 'Chilton', 'Eliah', 'Junior', 'Mark', 'Bond', 'Chick', 'Emmanuel', 'Raleigh', 'Brigham', 'Archibald', 'Gates', 'Filbert', 'Barnabas', 'Geoffrey', 'Terence', 'Stacy', 'Forbes', 'Gomer', 'Fairly', 'Archer', 'Oscar', 'William', 'Ernes', 'Chill', 'Gregory', 'Weylin', 'Holt', 'Clayland', 'Gram', 'Forbes', 'Set', 'Hartwell', 'Luke', 'Garson']
last_name = ['Mitchell', 'Martin', 'Anderson', 'Patel', 'Young', 'Jackson', 'Ward', 'Jackson', 'Patel', 'Walker', 'Lee', 'Patel', 'Johnson', 'Thomas', 'Morris', 'Watson', 'Martin', 'Roberts', 'Jones', 'Lewis', 'Morgan', 'Wood', 'Lee', 'White', 'James', 'Scott', 'Young', 'Clarke', 'Edwards', 'Smith', 'Jackson', 'Turner', 'Ward', 'Hall', 'Anderson', 'Walker', 'Scott', 'Mitchell', 'Williams', 'Young', 'Allen', 'Huges', 'Phillips', 'Robinson', 'Evans', 'Thomas', 'Taylor', 'Robinson', 'Harris', 'Ward', 'Johnson', 'Anderson', 'Scott', 'Martin', 'Allen', 'Clark', 'Jones', 'Wilson', 'Phillips', 'Lewis', 'Jones', 'Anderson', 'Wright', 'Clark', 'White', 'Lewis', 'Patel', 'Wilson', 'Wilson', 'Taylor', 'Williams', 'Turner', 'Smith', 'Davies', 'Harrison', 'Thompson', 'Anderson', 'Harris', 'Brown', 'Lewis', 'Phillips', 'Watson', 'Harrison', 'Harris', 'Wilson', 'Davies', 'Brown', 'Huges', 'Parker', 'King', 'Wright', 'Anderson', 'Anderson', 'Phillips', 'Harrison', 'Walker', 'Wood', 'Young', 'Clark', 'Jones']
troops = []
temp_platoon = []
platoons = []
platoon_names = ['Alpha', 'Beta', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel', 'India', 'Juliet', 'Kilo', 'Lima', 'Mike', 'November', 'Oscar', 'Papa', 'Quebec', 'Romeo', 'Sierra', 'Tango', 'Uniform', 'Victor', 'Whiskey', 'X-Ray', 'Yankee', 'Zulu']
a = 0
while a < 90:
    s_name = random.choice(first_name) + " " + random.choice(last_name)
    s_number = 100
    troops.append((s_name, s_number))
    a = a + 1


platoon_number = 1
troop_number = 0
for key in troops :
    troop_number = troop_number + 1
    a = troops.pop(0)
    temp_platoon.append((a))

    if troop_number == 30:
        # Platoon n is the name of  platoon
        # Platoon is the actual platoon
        platoon_number = platoon_number + 1
        platoon = platoon_names.pop(0)
        platoon_n = platoon
        platoon = []
        for k, v in temp_platoon:
            platoon.append((k, v)) 
            print("Added {} to platoon {}. He has {} health".format(k, platoon_n, v))
        platoons.append((platoon_n))
        troop_number = 0

def read ():
    print("Reading")
    for key in platoons:
        print(key)
        for w in key:
            print(w)
        print(platoons)

read()

另请注意,我正在自学 python 3。我才刚刚开始接触课程。

4

1 回答 1

0

我可能错了,但我想你可能希望它看起来像这样:

import random
first_name = ['John', 'Clay', 'Gordy', 'Erv']
last_name = ['Mitchell', 'Martin', 'Anderson', 'Patel']
platoon_names = ['Alpha', 'Beta', 'Charlie', 'Delta']
platoons = dict()
使用列表推导,我们可以完全替换 while 循环。每个士兵都是一个元组(姓名,健康)......我想你想要这个,但如果不只是删除 100)。
troops = [(random.choice(first_name) + " " + random.choice(last_name), 100) for r in range(90)]
以下辅助功能是从这里开始的升降机
def chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i+n]
我们现在填充排字典(键是排名,而值是包含士兵的列表)。
for number, platoon in enumerate(list(chunks(troops,3))):
    platoons[platoon_names[number]] = platoon
使用 ititems 遍历字典。
def read ():
    print("Reading")
    for platoon, troops in platoons.iteritems():
        print("--------Platoon name--------")
        print(platoon)
        print("--------troops--------")
        for (name, health) in troops:
            print(name)
这就是我们得到的。
>>> read()
Reading
--------Platoon name--------
Alpha
--------troops--------
John Anderson
Clay Mitchell
Erv Martin
--------Platoon name--------
Beta
--------troops--------
Clay Anderson
Clay Anderson
Clay Patel
--------Platoon name--------
Delta
--------troops--------
John Anderson
Gordy Martin
John Anderson
--------Platoon name--------
Charlie
--------troops--------
Clay Mitchell
Clay Patel
Clay Patel
>>> 
于 2013-09-13T22:58:33.990 回答