I am admittedly an extremely inexperienced programmer, who has been given the task of creating a Python program that will sort students into hotel rooms based on four choices of other students that they make (3 students they would like to. I am using a very elementary approach, seeing as I do not have the time nor abilities to create more advanced code. This is certainly a question that is coming from a beginner, but I have been trying to use the itertools.combinations() function to create a list of all possible student groups of length 4. To execute the following steps of my program, I need each of these lists of 4 students (each group) to be inside a larger list. Everything I have tried so far is not working, and my plan for the following code is reliant on creating a nested list of groups of 4.
for comb in itertools.combinations(names, 4):
combs = []
for list in comb:
combs.append(list)
print(combs)
Above is one of my closest attempts to creating my desired list, but the output is as such:
['Destinee', 'Dahlia', 'Rylie', 'Lauryn']
['Destinee', 'Dahlia', 'Rylie', 'Katelynn']
['Destinee', 'Dahlia', 'Rylie', 'Alondra']
['Destinee', 'Dahlia', 'Rylie', 'Amber']
['Destinee', 'Dahlia', 'Rylie', 'Diya']
['Destinee', 'Dahlia', 'Rylie', 'Delilah']
['Destinee', 'Dahlia', 'Rylie', 'Jazlyn']
['Destinee', 'Dahlia', 'Rylie', 'Alexus']
['Destinee', 'Dahlia', 'Rylie', 'Alissa']
['Destinee', 'Dahlia', 'Rylie', 'Alma']
['Destinee', 'Dahlia', 'Rylie', 'India']
['Destinee', 'Dahlia', 'Rylie', 'Kyla']
['Destinee', 'Dahlia', 'Rylie', 'Macy']
(and it continues like this for a while). How can I place each of these individual lists in a larger one?
import itertools
large_list = []
for comb in itertools.combinations(names, 4):
#combs = [] # this line is not needed
#for list in comb:
#combs.append(list) # neither is this one, if you print comb it is already a list
large_list.append(comb)
print(combs)
names = ['Peter', 'John', 'Angela', 'Jim', 'Derek', 'Gabriella']
combs = list(itertools.combinations(names, 4))
'''
combs :
[('Peter', 'John', 'Angela', 'Jim'),
('Peter', 'John', 'Angela', 'Derek'),
('Peter', 'John', 'Angela', 'Gabriella'),
('Peter', 'John', 'Jim', 'Derek'),
('Peter', 'John', 'Jim', 'Gabriella'),
('Peter', 'John', 'Derek', 'Gabriella'),
('Peter', 'Angela', 'Jim', 'Derek'),
('Peter', 'Angela', 'Jim', 'Gabriella'),
('Peter', 'Angela', 'Derek', 'Gabriella'),
('Peter', 'Jim', 'Derek', 'Gabriella'),
('John', 'Angela', 'Jim', 'Derek'),
('John', 'Angela', 'Jim', 'Gabriella'),
('John', 'Angela', 'Derek', 'Gabriella'),
('John', 'Jim', 'Derek', 'Gabriella'),
('Angela', 'Jim', 'Derek', 'Gabriella')]
Just add the combs list to the large_list
and it will save all combinations.
Or you can use list(itertools.combinations(names, 4))
which will also produce one large list.
ps never use list
as variable name, list
is a function in python.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.