创建列表列表 [英] Creating a list of lists

查看:75
本文介绍了创建列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

Loop that appends player names to a list called playerlist
print(playerlist)
lineuplist.append(playerlist)

print语句最终将打印如下列表:

The print statement ends up printing a list that looks like:

...
['Omer Asik', 'Jordan Farmar', 'Patrick Beverley', 'Kyle Korver', 'Kent Bazemore', 'Pau Gasol', 'Paul Millsap', 'Chandler Parsons', 'Kevin Durant']
['Omer Asik', 'Jordan Farmar', 'Patrick Beverley', 'Kyle Korver', 'Kent Bazemore', 'Serge Ibaka', 'Pau Gasol', 'Kevin Durant', 'Chandler Parsons']
['Omer Asik', 'Jordan Farmar', 'Patrick Beverley', 'Kyle Korver', 'Kent Bazemore', 'Serge Ibaka', 'Pau Gasol', 'Chandler Parsons', 'Kevin Durant']
['Omer Asik', 'Jordan Farmar', 'Patrick Beverley', 'Kyle Korver', 'Kent Bazemore', 'Paul Millsap', 'Pau Gasol', 'Kevin Durant', 'Chandler Parsons']
...

我希望我的输出是这些列表的列表,即:

I want my output to be a list of those lists, ie:

[['Omer Asik', 'Jordan Farmar', 'Patrick Beverley', 'Kyle Korver', 'Kent Bazemore', 'Pau Gasol', 'Paul Millsap', 'Chandler Parsons', 'Kevin Durant'],     ['Omer Asik', 'Jordan Farmar', 'Patrick Beverley', 'Kyle Korver', 'Kent Bazemore', 'Serge Ibaka', 'Pau Gasol', 'Kevin Durant', 'Chandler Parsons'], ['Omer Asik', 'Jordan Farmar', 'Patrick Beverley', 'Kyle Korver', 'Kent Bazemore', 'Serge Ibaka', 'Pau Gasol', 'Chandler Parsons', 'Kevin Durant'],     ['Omer Asik', 'Jordan Farmar', 'Patrick Beverley', 'Kyle Korver', 'Kent Bazemore', 'Paul Millsap', 'Pau Gasol', 'Kevin Durant', 'Chandler Parsons']]

但是,我所得到的只是这个:

However, all I get is this:

print(lineuplist)
[[], [], [], [], []]

不太确定我要去哪里错,因此可以提供任何帮助.

Not really sure where I'm going wrong, so any help is appreciated.

推荐答案

列表内容消失的实际问题已经在其他答案中得到解决,但是我想指出的是,您可以使代码变的很多更简单.

The actual problem with the content of the lists disappearing has already been solved in the other answer, however I want to note that you can make your code a whole lot simpler.

如果我正确地理解了代码(现在已经从问题中删除了),则您正在寻找的球员组合构成了一支法定阵容,其薪水在一定范围内.团队需要C组(队长?)中的一名球员,以及PGSGPFSF组中的每一个两名.

If I understand the code (now removed from the question) correctly, you are looking for combinations of players forming a legal lineup for a team whose combined salary is within certain bounds. The team needs one player from the C group (captain?), and 2 from each of the PG, SG, PF, and SF groups.

为此,您可以使用 itertools 模块,尤其是 product

You can use the itertools module for this, particularly product and combinations.

from itertools import product, combinations
player_combinations = product(combinations(C, 1), 
                              combinations(PG, 2),
                              combinations(SG, 2),
                              combinations(PF, 2),
                              combinations(SF, 2))

(更新:这将返回嵌套元组列表,因此您需要先对其进行展平.您可以使用

(Update: This returns nested lists of tuples, so you need to flatten it first. You can use sum for this.)

player_combinations = (sum(pc, ()) for pc in player_combinations)

结果是一个迭代器,其中包含播放器的所有组合(整个对象,而不仅仅是名称).现在,您可以在一个循环中找到满足薪水标准的球队,并提取他们的名字:

The result is an iterator with all the combinations of players (the entire objects, not just the names). Now you can find the teams that satisfy the salary criterion in just a single loop and extract their names:

lineuplist = []
for players in player_combinations:
    salary = sum(player[5] for player in players)
    if salarycap - undersalary <= salary <= salarycap:
        names = [player[1] for player in players]
        lineuplist.append(names)

这篇关于创建列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆