Generate all possible lists from the sublist in python [duplicate]

2024/11/15 22:29:56

Suppose I have list [['a', 'b', 'c'], ['d', 'e'], ['1', '2']]

I want to generate list where on the first place is any value from first sublist, on the second - from second etc.

So, a couple of examples:

['a', 'd', '1']
['b', 'd', '1']
['c', 'd', '1']
['a', 'e', '1']
['a', 'e', '2']
['b', 'e', '1']
['b', 'e', '2']

etc.

Answer

You need itertools.product() which return cartesian product of inpur iterables:

>>> from itertools import product
>>> my_list = [['a', 'b', 'c'], ['d', 'e'], ['1', '2']]#                v Unwraps the list
>>> list(product(*my_list))
[('a', 'd', '1'), ('a', 'd', '2'), ('a', 'e', '1'), ('a', 'e', '2'), ('b', 'd', '1'), ('b', 'd', '2'), ('b', 'e', '1'), ('b', 'e', '2'), ('c', 'd', '1'), ('c', 'd', '2'), ('c', 'e', '1'), ('c', 'e', '2')]
https://en.xdnf.cn/q/119602.html

Related Q&A

Time/frequency color map in python

Is there in native Python 3.X library or in scipy/numpy/matplolib libraries a function or their short set which could help me to draw a plot similar to this one(?):What would be an efficient way to ac…

ImageMagick is splitting the NASAs [.IMG] file by a black line upon converting to JPG

I have some raw .IMG format files which Im converting to .jpg using ImageMagick to apply a CNN Classifier. The converted images, however have a black vertical line splitting the image into two. The par…

CV2 - rectangular detecting issue

Im trying to implement an OMR using pythons CV2. As part of the code I need to find the corners of the choices box (rectangulars) however I ran into difficulty causes by the grade sheet template. In th…

Keras/TensorFlow - high acc, bad prediction

Im new to machine learning and Im trying to train a model which detects Prague city in a sentence. It can be in many word forms.Prague, PRAHA, Z Prahy etc...So I have a train dataset which consists of …

Flask-HTTPAuth: how to pass an extra argument to a function decorated with @auth.verify_password?

Heres a small Flask app authenticated with Flask-HTTPAuth. How to pass an argument (such as authentication on/off flag, or verbosity level / debug on/off flag) to a function (such as authenticate below…

AttributeError: numpy.ndarray object has no attribute split

Given a text file with one DNA sequence on each line and joining these together I now want to split the string into 5 sequences (corresponding to each of the 5 rows). This is the file source: http://ww…

How do I determine if a lat/long point is within a polygon?

I have a shapefile of all the counties that make up my state. Using the shapefile (which contains geometric for the district polygons) I was able to use geopandas to plot the shapes in a figure. I have…

How to return different types of arrays?

The high level problem Im having in C# is to make a single copy of a data structure that describes a robot control network packet (Ethercat), and then to use that single data structure to extract data …

How do I pass an array of strings to a python script as an argument?

Ive written a swift app that outputs an array of strings. I would like to import this array into a python script for further processing into an excel file via xlsxwriter, I would like to do this as an …

Match values of different dataframes

This dataframe is the principal with the original tweets. "original_ds_.csv" id tweet --------------------------------------------- 78 "onetoone"…