[Tutor] Creating lists with definite (n) items without repetitions

marcus lütolf marcus.luetolf at bluewin.ch
Fri Sep 4 16:28:10 CEST 2015


Hello Peter, hello Martin,
many thanks for your very quick response !!!

As for Peter's advice:

> At first I thought you might want itertools.combinations()
>
>>>> import string, itertools
>>>> for t in itertools.combinations(string.ascii_lowercase, 3):
> ...     print t # list(t) if you actually need a list
> ...
> ('a', 'b', 'c')
> ('a', 'b', 'd')
> ('a', 'b', 'e')
> ('a', 'b', 'f')
> ('a', 'b', 'g')
> [snip]
>
> but that gives
>
>>>> sum(1 for t in itertools.combinations(string.ascii_lowercase, 3))
> 2600

the 5 lists above do not match my task insofar as every of the 5 lists
contains  'a' and 'b' which should occur only once, hence my count of a maximum of 301 lists, which might nor be correct 100%.
My be one could put it in Python as follows:
> ('a', 'b', 'c') = True
> ('a', 'b', 'd')= False
> ('a', 'b', 'e')= False
> ('a', 'b', 'f')= False
> ('a', 'b', 'g')= False
I should probably tell you the real task are a series (maximum ~ 301) lists in which real names  of people are assigned to the items/letters for
2 people(golfers) can be in  the same list(flight)  only once for an extended period of time. 
The next step would be to assign compatible and noncompatible attributes  to the items/letters which will reduce the maximum of possible lists(flights)


As for Martin's advice:
You are using random.  Do you want n randomly selected items from the input list?  The random module provides random.sample() to select n items from a sequence.

If so, try this out at the intercative prompt.  Perhaps this is what you are looking for?

   >>> import random
   >>> import string
   >>> l = list(string.lowercase)
   >>> random.sample(l, 7)
   ['z', 'h', 'e', 'n', 'c', 'f', 'r']

The two modules random and string should provide 7 ore more sets of 3 items not just one (letter ['z',..................'r']) like
['z', 'a', 'b'], ['h','a','c'], ['e','a','d'], ['n','a','f'],.........['r','a','g'].

I hope I could make myself clear(er) and  used the appropriate format to communicate.
Thanks again for your help, Marcus
...........................................................................................................................................................................

-----Ursprüngliche Nachricht-----
Von: marcus.luetolf at bluewin.ch [mailto:marcus.luetolf at bluewin.ch] 
Gesendet: Freitag, 4. September 2015 15:27
An: marcus.luetolf at bluewin.ch
Betreff: Fwd: Creating lists with definite (n) items without repetitions


----Ursprüngliche Nachricht----
Von : marcus.luetolf at bluewin.ch
Datum : 03/09/2015 - 15:32 (UTC)
An : tutor at python.org
Betreff : Creating lists with definite (n) items without repetitions

dear pythonistas
as a newcomber I want to create a set of lists containing n items, for example n = 3:  (['a','b','c'], ['a','d','e'].......).
The sequence of items in each list should be different. If the letters 'a'........'z' are used and n = 3 there is a maximum of 301 lists.
The following code works only for lists containing 1 item:

import random
list = ['a', 'b', 'c', 'd',....... 'z']
random.shuffle(list)
for x in list:
     print x

how can I solve my task wit n items ?
Thank you for help, Marcus.



---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus



More information about the Tutor mailing list