Python 3:打印列表中的列表

时间:2013-09-13 21:59:59

标签: python list printing

我似乎无法打印列表中的列表。我知道当我为键和值重复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.我刚开始接触课程。

1 个答案:

答案 0 :(得分: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)]
以下辅助函数是来自here的升降器
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
使用iteritems迭代字典。
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
>>>