简体   繁体   English

Python:在空列表中创建空列表?

[英]Python: creating empty lists within empty lists?

EDIT: so I figured out that if I declare this at the beginning it works fine: 编辑:所以我发现,如果我在一开始就声明了它,效果很好:

RelayPlaceHolder = [[],[],[],[],[],[],[],[],[]]

Why can't something like this create the same sort of empty containers? 为什么这样的东西不能创建相同类型的空容器? the number of empty lists might change: 空列表的数量可能会更改:

for SwimTeams in SwimTeamList:
                empty = []
                RelayPlaceHolder.append(empty)

this was my old question... 这是我的老问题...


I have a list of lists of further lists of single dictionaries: 我有一个列表,进一步列出了单个词典:

TeamAgeGroupGender[team#][swimmer#][their dictionary with a key such as {"free":19.05}]

I have a loop that for every team in the first level of my lists, it then loops through every swimmer within that team's list, and add's their swim that corresponds to the key value "free" to a new list called RelayPlaceHolder[teamid][***the thing I just added***] 我有一个循环,对于列表中第一级的每个团队,然后循环浏览该团队列表中的每个游泳者,并将与键值“ free”相对应的他们的游泳添加到名为RelayPlaceHolder[teamid][***the thing I just added***]

    for SwimTeams in SwimTeamList:
            empty = []
            RelayPlaceHolder.append(empty)
            teamid = SwimTeamList.index(SwimTeams)
            print SwimTeams
            print teamid

            for swimmers in TeamAgeGroupGender[teamid]:
                swimmerID = TeamAgeGroupGender[teamid].index(swimmers)
                RelayPlaceHolder[teamid].append({SwimTeams:TeamAgeGroupGender[teamid][swimmerID]["Free"]})
            print RelayPlaceHolder[teamid][0]

Desired: 期望:

RelayPlaceHolder[0][*** list of swims that go with this team#0 ***] 
RelayPlaceHolder[1][*** list of swims that go with team#1, ie the next teamid in the loop***]

for some reason, my loop is only adding swims to RelayPlaceHolder[0] , even the swims from team#1. 出于某种原因,我的循环仅将游泳添加到RelayPlaceHolder[0] ,甚至来自团队#1的游泳。 I tried using the print to troubleshoot, however, the teamid index and swimteam names are changing just fine from #0 to #1, however, my RelayPlaceHolder[teamid].append is still adding to the #0 list and not the #1. 我尝试使用打印进行故障排除,但是,组teamid索引和swimteam名称从#0变为#1很好,但是,我的RelayPlaceHolder[teamid].append仍添加到#0列表中,而不是#1。 I also know this because a key value from later code is failing to find the correct key in RelayPlaceHolder[1] (because its turning up empty). 我也知道这一点,因为后面代码中的键值无法在RelayPlaceHolder[1]找到正确的键(因为它变成空的)。 I'm not sure why my loop is failing. 我不确定为什么我的循环失败了。 I've used similar structure in other loops... 我在其他循环中使用了类似的结构...

Thank you. 谢谢。

As commented by @doukremt: A concise syntax if you need to define an arbitrary number of lists is: 正如@doukremt所评论的:如果需要定义任意数量的列表,则简洁的语法为:

[[] for i in range(some_number)]

If you need to do it more often, you can implement it in a function: 如果您需要更频繁地执行此操作,可以在一个函数中实现它:

>>> lists = lambda x: [[] for i in range(x)]
>>> lists(3)
[[], [], []]

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

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