How to get all possible combinations of three lists [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 15 days ago.
Improve this question
I have three lists:
list_a=[1,2]
list_b=[3,4]
list_c=[5,6]
Now i'm looking for a solution in python by using a loop where a new list iterates through all
possible combinations of these three lists (the order is not important):
new_list = list_a # -> [1,2]
new_list = list_b # -> [3,4]
new_list = list_c # -> [5,6]
new_list = list_a + list_b # -> [1,2,3,4]
new_list = list_b + list_c # -> [3,4,5,6]
new_list = list_a + list_c # -> [1,2,5,6]
new_list = List_a + list_b + list_c # -> [1,2,3,4,5,6]
I found some similar posts with "itertools" or nested loops, but nothing exactly like this...

You can use itertools:
from itertools import combinations, chain
out = [list(chain.from_iterable(l))
for i in range(3)
for l in combinations([list_a, list_b, list_c], i+1)]
# Output
[[1, 2],
[3, 4],
[5, 6],
[1, 2, 3, 4],
[1, 2, 5, 6],
[3, 4, 5, 6],
[1, 2, 3, 4, 5, 6]]

Related

How do I find indexes of one list based on values of another list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to obtain indexes of one list based on elements of another list.
Let's say I have:
list1 = [1, 2]
list2 = [1, 2, 2, 3]
I want to obtain indexes of list2 that match elements from list1:
>>> [0, 1, 2]
Is there any one liner that can do it? Thank you for any suggestions.
EDIT:
Current multiple line solution:
list1 = [1, 2]
list2 = [1, 2, 2, 3]
matched = []
for i, e in enumerate(list1):
for j, f in enumerate(list2):
if e == f:
matched.append(j)
>>> [0, 1, 2]
For relatively short lists, you can use a list comprehension:
>>> list1 = [1, 2]
>>> list2 = [1, 2, 2, 3]
>>> [i for i, l in enumerate(list2) if l in list1]
[0, 1, 2]
If your lists contain thousands of elements or more, you should consider using numpy instead.
Try:
>>> [i for i, x in enumerate(list2) if x in list1]
[0, 1, 2]
how about something like this:
print([i for i in range(len(list2)) if list2[i] in list1])

Extracting a part of the list based on a criteria [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
lst = [[2,'UNIT'], [5,'TEN'], [8,'HUNDRED'], [7, 'THOUSAND'], [8, 'THOUSAND'], [9,'LAKH'],
[6, 'LAKH'], [4, 'CRORE'], [2, 'CRORE'], [3, 'CRORE']]
I have 10 lists inside list named lst.
I want to iterate through the list using a for loop for all the list with a specific value, say CRORE, instead of the entire list.
This might help. I have written the code below:
lst = [[2,'UNIT'], [5,'TEN'], [8,'HUNDRED'], [7, 'THOUSAND'], [8, 'THOUSAND'],
[9,'LAKH'], [6, 'LAKH'], [4, 'CRORE'], [2, 'CRORE'], [3, 'CRORE']]
for x in lst:
for y in x:
if y=="CRORE":
print(x)
Here x represents all the sublists inside your list i.e lst,
and y represents the values inside the sublists i.e integers or strings.
Also this can be done using list comprehension as mentioned below
in this you will get a new list with the values of CRORE only.
lst = [[2,'UNIT'], [5,'TEN'], [8,'HUNDRED'], [7, 'THOUSAND'], [8, 'THOUSAND'],
[9,'LAKH'], [6, 'LAKH'], [4, 'CRORE'], [2, 'CRORE'], [3, 'CRORE']]
newlst = [x for x in lst if x[1] == 'CRORE']
print(newlst)
2 ways to filter the list of list based on the value at index 1:
filtered_list = list(filter(lambda x: x[1] == 'CRORE', lst))
filtered_list = [i for i in lst if i[1] == 'CRORE']
iterate thorugh your list and it contains 2 elements in list so get second position and use if conditon and create another list and append data to it
lst = [[2,'UNIT'], [5,'TEN'], [8,'HUNDRED'], [7, 'THOUSAND'], [8, 'THOUSAND'], [9,'LAKH'], [6, 'LAKH'], [4, 'CRORE'], [2, 'CRORE'], [3, 'CRORE']]
lst1=[]
for i,j in lst:
if j=='CRORE':
lst1.append([i,j])
in one line answer for list comprehension
lst1=[[i,j] for i,j in lst if j=="CRORE"]
Output:
[[4, 'CRORE'], [2, 'CRORE'], [3, 'CRORE']]

Is there a method to create a sublist from the following list_A with the output as follows in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to create a sublist from the list as [(INDEX, NUMBER), (INDEX, NUMBER)...]
FOR EXAMPLE
list_A = [0,1,2,3,4,5]
output = [(0,1),(1,2),(3,4),(4,5),(5)]
It's very much doable using list itself:
#Original list:
li = [1,2,3,4,5,6,7,8,9]
#How many values would we need to make combination of:
num = 2
li2 = [li[a:a+num] for a in range(len(li))]
print(li2)
Output:
[[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9]]
If you want to make list of list using 3 values, just change num to 3 and so on. I am pretty sure there will definitely be a numpy function or some other pythonic way to do that same though.
list_A = [0, 1, 2, 3, 4, 5]
output = [list_A[i:i + 2] for i in range(0, len(list_A), 2)]
You can do also using lambda function
import itertools
def grouper(n, it):
it = iter(it)
return iter(lambda: list(itertools.islice(it, n)), [])
list(grouper(2, list_A))

replace variables in a list with combinations of elements [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to replace variables in a list with combination of elements.
To be more specific:
I have these two lists
liste1 = [1,2,3,'X','X',4]
liste2 = [5,6,7]
and I want to get a list containing the elements below :
[1,2,3,5,6,4]
[1,2,3,5,7,4]
[1,2,3,6,7,4]
[1,2,3,6,5,4]
[1,2,3,7,5,4]
[1,2,3,7,6,4]
Does anyone have an idea how to make it ?
You can do it this way:
from itertools import permutations
liste1 = [1, 2, 3, 'X', 'X', 4]
liste2 = [5, 6, 7]
def replacements(liste1, liste2):
x_indices = [i for i, val in enumerate(liste1) if val == 'X']
nb = len(x_indices)
for perm in permutations(liste2, nb):
l1 = liste1[:] # if we want to preserve the original and yield different lists
for i, new_val in zip(x_indices, perm):
l1[i] = new_val
yield l1
for r in replacements(liste1, liste2):
print(r)
Output:
[1, 2, 3, 5, 6, 4]
[1, 2, 3, 5, 7, 4]
[1, 2, 3, 6, 5, 4]
[1, 2, 3, 6, 7, 4]
[1, 2, 3, 7, 5, 4]
[1, 2, 3, 7, 6, 4]
We first list the indices where 'X' appears, then generate the permutations of as many elements of liste2. For each permutation, we replace the 'X's.

How to delete the elements from one list which aren't contained in another list? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have two lists of lists:
arr1 = [[1,2,3],
[2,5,1,1],
[3,1,1]]
arr2 = [[2,3,6,1],
[8,1,3],
[5,5,6]]
I need to check which elements from arr2 aren't contained in arr1 and delete those elements from arr2.
So result must be:
arr2 = [[2,3,1],
[1,3],
[5,5]]
6 and 8 aren't contained in arr1, so it deleted in arr2.
How to do that?
arr1 = [[1, 2, 3],
[2, 5, 1, 1],
[3, 1, 1]]
arr2 = [[2, 3, 6, 1],
[8, 1, 3],
[7, 5, 6]]
set1 = set(sum(arr1, []))
print('Elements found in arr1:')
print(set1)
arr3 = [[x for x in sub if x in set1]
for sub in arr2]
print('Sublists of arr3:')
for sub in arr3:
print(sub)
Output:
Elements found in arr1:
set([1, 2, 3, 5])
Sublists of arr3:
[2, 3, 1]
[1, 3]
[5]

Categories

Resources