问题
I have code that looks like:
Loop that appends player names to a list called playerlist
print(playerlist)
lineuplist.append(playerlist)
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.
回答1:
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.
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.
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)
回答2:
When you append the list into your main list, you're actually appending a pointer to that original list. Python lists are mutable, and when you change them, you change them wherever you have a pointer to them.
If you append a copy of them, you can avoid this being an issue, but you'll waste memory, so it's important to have a good reason to copy them.
Here's an example of this behavior:
>>> l = list('abc')
>>> m = [l.copy()]
>>> m
[['a', 'b', 'c']]
>>> l.remove('a')
>>> m
[['a', 'b', 'c']]
>>> l
['b', 'c']
So what you could do is this:
lineuplist.append(playerlist.copy())
instead of this:
lineuplist.append(playerlist)
来源:https://stackoverflow.com/questions/22404726/creating-a-list-of-lists