Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
itertools: combinations
#1
i'm looking over the various functions in itertools but i'm not finding what i need.

my use case is to take a list of lists of strings and iterate over the 2nd level to produce a list of the combination of strings. the incoming list might be:

[  ['foo','bar'], ['+','-'], ['corn','wheat','rice'] ]
the first 3 and last 3 iterations (in a list) would be:

[
['foo','+','corn'],
['bar','+','corn'],
['foo','-','corn'],
...
['bar','+','rice'],
['foo','-','rice'],
['bar','-','rice'],
]
the whole big list of 12 (in this example) lists is what would be returned. the generator version of this would do a yield of each iteration for a total of 12 yields. better code would be agnostic about what kind of data object or reference is used in place of the strings. i was trying to write this myself. maybe i should go back to that.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You're looking for itertools.product().
>>> import itertools
>>> for x in itertools.product(['foo', 'bar'], ['+', '-'], ['corn', 'wheat', 'rice']):
...     print(x)
...
('foo', '+', 'corn')
('foo', '+', 'wheat')
('foo', '+', 'rice')
('foo', '-', 'corn')
('foo', '-', 'wheat')
('foo', '-', 'rice')
('bar', '+', 'corn')
('bar', '+', 'wheat')
('bar', '+', 'rice')
('bar', '-', 'corn')
('bar', '-', 'wheat')
('bar', '-', 'rice')
Reply
#3
the order is different, but that's ok. itertools.product cycles the last list faster and my example cycles the first list faster. for my needs, it doesn't matter.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding combinations of list of items (30 or so) LynnS 1 876 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  itertools and amazing speed Pedroski55 8 2,053 Nov-11-2022, 01:20 PM
Last Post: Gribouillis
  How can I find all combinations with a regular expression? AlekseyPython 0 1,676 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  All possible combinations CODEP 2 1,860 Dec-01-2020, 06:10 PM
Last Post: deanhystad
  What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? Pedroski55 3 2,400 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Triplet Combinations of All Values quest 2 1,982 Nov-05-2020, 09:22 AM
Last Post: quest
  Making lists using itertools and eliminating duplicates. mike3891 2 2,244 Oct-26-2020, 05:39 PM
Last Post: bowlofred
  Python3 itertools.groupby printing the key august 1 2,089 Aug-17-2020, 05:46 AM
Last Post: bowlofred
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 1,843 Jun-04-2020, 04:51 PM
Last Post: CoderMan
  All possible combinations of multiplications Shreya10o 0 1,669 May-23-2020, 07:45 AM
Last Post: Shreya10o

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020