How to generate a permutation of list of lists in python

2024/11/14 18:31:20

I have a list of lists say

[[2, 4, 6], [2, 6, 10], [2, 12, 22], [4, 6, 8], [4, 8, 12], [6, 8, 10], [8, 10, 12], [8, 15, 22], [10, 11, 12]]

How do I generate a combination of the lists for a given length?

For example if the given length is 3

Then I need all combinations of 3 list element scenarios from the given list of list.

Example :

[2, 4, 6], [2, 6, 10], [2, 12, 22]

or

[2, 4, 6], [8, 10, 12], [10, 11, 12]
...
... and so forth

and if the given length is 2 then it should be like

[2, 4, 6], [2, 6, 10]

or

[6, 8, 10], [8, 10, 12]
...
... and so forth

I dont want a permutation of elements inside the lists but I want a permutation of the lists itself.

Answer

There are two options here. The first is to use itertools.permutations. This will give you every permutation (i.e: [1,2] and [2,1] would not be the same)

import itertoolslists = [[2, 4, 6], [2, 6, 10], [2, 12, 22], [4, 6, 8], [4, 8, 12], [6, 8, 10], [8, 10, 12], [8, 15, 22], [10, 11, 12]]
n = 3for perm in itertools.permutations(lists, n):print(perm)

If you want completely unique groupings, with no duplicates , use itertools.combinations (i.e: [1,2] and [2,1] would be the same).

for comb in itertools.combinations(lists, n):print(comb)
https://en.xdnf.cn/q/119097.html

Related Q&A

Issue sending file via Discord bot (Python)

if message.content.upper().startswith("!HEADPATS"):time.sleep(1)with open(tenor.gif, rb) as picture:await client.send_file(channel, picture)Ive got my discord bot up and running (everythings …

Matplotlib installation on Mavericks

Im having problem while installing matplotlib. Im using Mavericks and it complains about a deprecated NumPy API both installing via pip and installing from source (following the instructions here https…

Exact string search in XML files?

I need to search into some XML files (all of them have the same name, pom.xml) for the following text sequence exactly (also in subfolders), so in case somebody write some text or even a blank, I must …

Integrate a function by the trapezoidal rule- Python

Here is the homework assignment Im trying to solve:A further improvement of the approximate integration method from the last question is to divide the area under the f(x) curve into n equally-spaced tr…

Kivy module not found in vscode (Mac)

I have installed Kivy and when I used the IDLE app that came with Python I can import it and it runs perfectly. However, when I try to import it in vscode I get the error: ModuleNotFoundError: No modul…

How to get latest unique entries from sqlite db with the counter of entries via Django ORM

I have a SQLite db which looks like this:|ID|DateTime|Lang|Details| |1 |16 Oct | GB | GB1 | |2 |15 Oct | GB | GB2 | |3 |17 Oct | ES | ES1 | |4 |13 Oct | ES | ES2 | |5 |15 Oct | ES | ES3 …

What does this code %.8f% do in python? [duplicate]

This question already has answers here:What does % do to strings in Python? [duplicate](4 answers)Closed 6 years ago.I am editing a code line to pass the rate in quotes:OO000OO00O0O0O000 [rate]=O0O0OO…

How to append a selection of a numpy array to an empty numpy array

I have a three .txt files to which I have successfully made into a numpy array. If you are curious these files are Level 2 data from the Advanced Composition Experiment (ACE). The particular files are …

Error saving and loading a list of matrices

I have a list "data_list", and I would save it in order to load it in another script. First of all I converted it in an array, in this way:data_array = np.array(data_list)Then I saved it:np.s…

Trying to interact with HTML page elements, but none of them are found

Im trying to scrape a webpage using Selenium, but when I try to pass the XPath of a button, I get an error saying that this element does not exist. I tried with another website, and it worked perfectly…