[Tutor] Creating lists with 3 (later4) items occuring only once
marcus lütolf
marcus.luetolf at bluewin.ch
Sun Sep 20 17:47:01 CEST 2015
Hi Alan G,
thanks for your quick answer.
The copy of my code below, sent on 18/09/15 16:41 is for some reasons incomplete. It should read
>>> import string, itertools
>>> s = ['ab','ac','bc','ad','ae','de']
>>> count = 0
>>> for startlist in itertools.combinations(s, 3):
>>> count = count + 1
>>> stl = list(startlist)
>>> print count, stl
>>> for pair in s:
>>> x = stl.count(pair)
>>> print x, pair
My task is to have lists containing 3 tuples, each tuple ocurring only once. In my code above I started off with 6 tuples ( = s) of which 20
lists (count) are produced containing each of the 6 tuples multiple times.
Therefore, I try to write additional code for eliminating all lists which contain tuples occuring more than once.
If I use all 325 tuples I would first get many thousand lists.
Your code proposal uses dictionaries. If I used dictionaries with all the 325 possible tuples I would have to do a lot of typing and not
getting lists with 3 tuples in it.
So, I am stuck somehow and hope to have made my tasks clear.
Thanks for further help, Marcus.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----Ursprüngliche Nachricht-----
Von: Tutor [mailto:tutor-bounces+marcus.luetolf=bluewin.ch at python.org] Im Auftrag von Alan Gauld
Gesendet: Freitag, 18. September 2015 18:58
An: tutor at python.org
Betreff: Re: [Tutor] Creating lists with 3 (later4) items occuring only once
On 18/09/15 16:41, marcus lütolf wrote:
>>>> s = ['ab','ac','bc','ad','ae','de'] for startlist in
>>>> itertools.combinations(s, 3):
> How can I concatenate the 20 lists in oder to get one count for each of the items in s , for example 10 for 'ab'?
If I understand you correctly, something like this:
>>> counts = {'ab':0,'ac':0,'bc':0,'ad':0,'ae':0,'de':0}
>>> for combo in it.combinations(counts.keys(),3):
... for pair in combo:
... counts[pair] += 1
...
>>> counts
{'ac': 10, 'ab': 10, 'ae': 10, 'ad': 10, 'bc': 10, 'de': 10}
Is that what you want?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus
More information about the Tutor
mailing list