Adding elements of list to list not working - python

I have a list which contains lists,
testList = [[1, 0, 1], [1, 1, 1], [0, 0, 0], ...]
What I want to do is that add the list in groups of n, so if n is 2, the first two lists will be added together and the second two lists will be added together, it will be like [1, 0, 1] + [1, 1, 1] = [2, 1, 2].
I tried the following code,
group_len = 3
sumResultList = [[sum(x) for x in zip(*testList)] for group in [testList[i:i+group_len] for i in range(0, len(testList), group_len)]]
But the above code does not work, how do I solve this?

l = [[1, 0, 1], [1, 1, 1],
[0, 0, 0], [1, 0, 1],
[1, 1, 1], [1, 0, 1],
[1, 1, 1], [1, 1, 1]]
n = 2
print [[sum(x) for x in zip(*l[a:a+n])] for a in range(0,len(l),n)]
Output:
[[2, 1, 2], [1, 0, 1], [2, 1, 2], [2, 2, 2]]

Try this:
testList = [[1, 0, 1], [1, 1, 1], [0, 0, 0], [2,3,4], [2,3,4], [2,3,4]]
group_len = 2
res = [list(map(sum, zip(*testList[k:k+group_len]))) for k in range(0, len(testList), group_len)]
print(res)
output:
[[2, 1, 2], [2, 3, 4], [4, 6, 8]]

Related

How to check if a list exists in a list of lists? [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Check if list of lists of lists contains a specific list
(3 answers)
Closed 11 days ago.
I have an if statement:
if lst not in listOfLists:
And this if statement is not catching if the list for example:
lst = [[1, -1, -1, 1, 1], [1, 2, 2, 0, 1], [1, 4, 3, 0, 1], [1, 1, 1, 1, 1]]
is in a list of lists for example:
listOfLists = [[[1, -1, -1, 1, 1], [1, 0, 3, 4, 1], [1, 0, 2, 2, 1], [1, 1, 1, 1, 1]], [[1, -1, -1, 1, 1], [1, 3, 0, 4, 1], [1, 0, 2, 2, 1], [1, 1, 1, 1, 1]], [[1, -1, -1, 1, 1], [1, 0, 3, 4, 1], [1, 2, 2, 0, 1], [1, 1, 1, 1, 1]], [[1, -1, -1, 1, 1], [1, 0, 0, 4, 1], [1, 3, 2, 2, 1], [1, 1, 1, 1, 1]], [[1, -1, -1, 1, 1], [1, 3, 4, 0, 1], [1, 0, 2, 2, 1], [1, 1, 1, 1, 1]], [[1, -1, -1, 1, 1], [1, 3, 0, 4, 1], [1, 2, 2, 0, 1], [1, 1, 1, 1, 1]], [[1, -1, -1, 1, 1], [1, 3, 0, 4, 1], [1, 2, 2, 0, 1], [1, 1, 1, 1, 1]], [[1, -1, -1, 1, 1], [1, 0, 3, 0, 1], [1, 2, 2, 4, 1], [1, 1, 1, 1, 1]], [[1, -1, -1, 1, 1], [1, 0, 4, 0, 1], [1, 3, 2, 2, 1], [1, 1, 1, 1, 1]], and so on
I've tried changing the listOfLists to a set, and converting the lst to a tuple in the if statement. I've tried collections, all, and any.
To clarify, lst is definitely in listOfLists, but it's not finding it
This code works for me, you likely don't have your list in the big list.
ls = [[1, 2], [3, 4]]
ls = [[1, 2], [3, 5]]
lsls = [ [[1, 2], [3, 4]], [[5,6], [7, 8]] ]
if ls in lsls:
print("yes")
else:
print("not in")
if ls2 in lsls:
print("yes")
else:
print("not in")
prints: yes, not in

how to compare and count number of occurrences in two list of list?

I have two different list of list, with different size (List A with size in range 1000 and list B with size in range 10,000).
A=[[0, 0, 0],
[0, 0, 1],
[0, 0, 2],
[0, 0, 3],
[0, 0, 4],
[0, 0, 5],
[0, 1, 0],
[0, 1, 1],
[0, 1, 2],
[0, 1, 3],
[0, 1, 4],
[0, 1, 5],
[0, 1, 6],
[0, 1, 7],
[0, 1, 8],
[0, 1, 9],
[0, 2, 0],
[0, 2, 1],
[0, 2, 2]]
B=[[1, 1, 2],
[0, 0, 2],
[0, 0, 1],
[4, 2, 2],
[3, 1, 2],
[1, 0, 1],
[1, 1, 2],
[0, 1, 2],
[0, 0, 0],
[2, 2, 3],
[1, 2, 1],
[0, 2, 1],
[0, 2, 0],
[0, 2, 1],
[0, 1, 3],
[0, 0, 0],
[1, 2, 5],
[0, 4, 3],
[0, 1, 3]]
I need to compare list with list B and find out how many times each element of A occur in B. For example I need to find out how many times [0,0,0] (first element of A) occur in B.
Thank you for your help.
This should work:
A = [[0, 0, 0],
[0, 0, 1],
[0, 0, 2],
[0, 0, 3],
[0, 0, 4],
[0, 0, 5],
[0, 1, 0],
[0, 1, 1],
[0, 1, 2],
[0, 1, 3],
[0, 1, 4],
[0, 1, 5],
[0, 1, 6],
[0, 1, 7],
[0, 1, 8],
[0, 1, 9],
[0, 2, 0],
[0, 2, 1],
[0, 2, 2]]
B = [[1, 1, 2],
[0, 0, 2],
[0, 0, 1],
[4, 2, 2],
[3, 1, 2],
[1, 0, 1],
[1, 1, 2],
[0, 1, 2],
[0, 0, 0],
[2, 2, 3],
[1, 2, 1],
[0, 2, 1],
[0, 2, 0],
[0, 2, 1],
[0, 1, 3],
[0, 0, 0],
[1, 2, 5],
[0, 4, 3],
[0, 1, 3]]
nums = []
for i in A:
for j in B:
if i in B:
nums.append(str(i))
nums_freq = {}
nums = list(dict.fromkeys(nums))
for i in nums:
count = 0
for j in B:
if i == str(j):
if i in nums_freq.keys():
nums_freq[i] += 1
else:
nums_freq[i] = 1
Value for num_freq:
{'[0, 0, 0]': 2,
'[0, 0, 1]': 1,
'[0, 0, 2]': 1,
'[0, 1, 2]': 1,
'[0, 1, 3]': 2,
'[0, 2, 0]': 1,
'[0, 2, 1]': 2}
>>> import operator
>>> for e in A:
... print(e, 'appearing in :', operator.countOf(B, e))
...
[0, 0, 0] appearing in : 2
[0, 0, 1] appearing in : 1
[0, 0, 2] appearing in : 1
[0, 0, 3] appearing in : 0
[0, 0, 4] appearing in : 0
[0, 0, 5] appearing in : 0
[0, 1, 0] appearing in : 0
[0, 1, 1] appearing in : 0
[0, 1, 2] appearing in : 1
[0, 1, 3] appearing in : 2
[0, 1, 4] appearing in : 0
[0, 1, 5] appearing in : 0
[0, 1, 6] appearing in : 0
[0, 1, 7] appearing in : 0
[0, 1, 8] appearing in : 0
[0, 1, 9] appearing in : 0
[0, 2, 0] appearing in : 1
[0, 2, 1] appearing in : 2
[0, 2, 2] appearing in : 0
I can suggest 2 different ways to do it:
Using a counter:
from collections import Counter
cb = Counter(tuple(b) for b in B)
list((a, cb[tuple(a)]) for a in A))
Using nested comprehensions:
list((a, sum(all(ia == ib for ia, ib in zip(a, b)) for b in B)) for a in A)
Actually, you should consider having the inner collection as tuples instead of lists, because tuples support element-wise equality and are hashable.
if A and B were lists of tuples, it would be as simple as:
Counter(a for a in A for b in B if a == b)

How to remove elements from list in array without using for-loop [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 3 years ago.
Improve this question
I have this code:
portes = [[0, 1, 2]] * 20
bonnes_portes = np.random.choice(range(3), size=(1, 20))
premier_choix = np.random.choice(range(3), size=(1, 20))
print(portes)
print(premier_choix)
this outputs:
[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]
[[1 1 1 2 2 1 1 2 2 1 0 0 1 2 0 0 1 2 1 2]]
I want to remove every element of premier_choix list from portes lists sequentially (remove premier_choix[0][0] from portes[0][0] ...) without using for loop.
You can use the np.argwhere argument to get the relevant indices then use a slicing operation on your array and reshape it back to the desired form.
portes_arr = np.array(portes)
idx = np.argwhere(portes_arr != np.array(premier_choix).reshape(20,1))
portes_arr[idx[:,0], idx[:,1]].reshape(20,2)
(Example-)Inputs
premier_choix
array([[2, 1, 0, 1, 1, 2, 2, 1, 1, 1, 1, 2, 1, 0, 1, 2, 2, 2, 1, 1]])
Outputs
portes_arr[idx[:,0], idx[:,1]].reshape(20,2)
array([[0, 1],
[0, 2],
[1, 2],
[0, 2],
[0, 2],
[0, 1],
[0, 1],
[0, 2],
[0, 2],
[0, 2],
[0, 2],
[0, 1],
[0, 2],
[1, 2],
[0, 2],
[0, 1],
[0, 1],
[0, 1],
[0, 2],
[0, 2]])
I asssume there is always an element to remove (like your example above). The input:
# Import
import numpy as np
# Input
portes = [[0, 1, 2]] * 20
premier_choix = np.random.choice(range(3), size=(1, 20))
# Modify input (or start with it)
portes = np.array(portes)
premier_choix = premier_choix.reshape(-1, 1)
A vectorized solution:
output = portes[portes != premier_choix].reshape(-1, 2)
print(output)
You can use .remove() to delete the list altogether

How to calculate all the combinations of lists

I am blocked on a Python problem and hope someone could help me.
The problem is prety simple actually.
Im trying to build lists with all combination possible but the elements of the list have not the same range.
here is my code, I tried to do something with for loop but it doesnt work.
for j in range(0,size):
for k, val in enumerate(self.Algo.Inputs[j].Values):
self.Commandlist[j] = k
self.Commandlist is a list with fix range, and fill with zero at first.
self.Commandlist = [0,0,0]
self.Algo.Inputs[j].Values gives me the size of each elements, for example, if self.Algo.Inputs[0].Values = 4
self.Algo.Inputs[1].Values = 1
self.Algo.Inputs[2].Values = 2
i want all the combinations, [0,0,0],[1,0,0],[2,0,0],[3,0,0],[4,0,0],[0,1,0],[1,1,0],[2,1,0],[3,1,0],[4,1,0] etc..
I think I forgot a loop but i cant figure out. I tried some stuff with itertools module as well, but i cant make it work.
Thans for your help.
As mentioned you can use itertools, for example like that:
import itertools
a = b = c = range(3) # you can specify different range for each one
[list(x) for x in list(itertools.product(a, b, c))]
Result:
[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 2, 0], [0, 2, 1], [0, 2, 2], [1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 1, 0], [1, 1, 1], [1, 1, 2], [1, 2, 0], [1, 2, 1], [1, 2, 2], [2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 1, 0], [2, 1, 1], [2, 1, 2], [2, 2, 0], [2, 2, 1], [2, 2, 2]]

How can I count duplicates in a nested list based on first two elements in python

I have a list in the form:
lst = [[1, 0, 0, 0], [1, 1, 0, 0], [2, 0, 0, 0], [2, 1, 0, 0], [2, 1, 0, 0], [1, 1, 0, 0], [3, 1, 0, 0], [1, 3, 0, 0], [2, 1, 0, 0], [2, 0, 0, 0]]
However the last two sub-elements will always be zero at the start so it could be like:
lst = [[1, 0], [1, 1], [2, 0], [2, 1], [2, 1], [1, 1], [3, 1], [1, 3], [2, 1], [2, 0]]
If that is easier.
What I want is to remove and count the duplicates of this list and set the 3rd sub-element to the count so if we take the above I want:
lst = [[1, 0, 1, 0], [1, 1, 2, 0], [2, 0, 2, 0], [2, 1, 3, 0], [3, 1, 1, 0], [1, 3, 1, 0]]
I have found explanations of how to remove duplicates at:
Removing Duplicates from Nested List Based on First 2 Elements
and
Removing duplicates from list of lists in Python
but I don't know how to count the duplicates. The order of the elements in the overall list doesn't matter but the order of the elements in the sub-lists must be preserved as [1,3] and [3,1] aren't the same thing.
If this turns out to be a dead end I could do something like hash the first two elements for counting but only if I could get them back after counting.
Any help is appreciated.
Sorry for dyslexia!
For example:
lst = [[1, 0, 0, 0], [1, 1, 0, 0], [2, 0, 0, 0], [2, 1, 0, 0], [2, 1, 0, 0], [1, 1, 0, 0], [3, 1, 0, 0], [1, 3, 0, 0], [2, 1, 0, 0], [2, 0, 0, 0]]
from collections import Counter
c = Counter(tuple(i) for i in lst)
print [list(item[0][0:2] + (item[1], 0)) for item in c.items()]
# [[1, 0, 1, 0], [1, 1, 2, 0], [3, 1, 1, 0], [2, 1, 3, 0], [1, 3, 1, 0], [2, 0, 2, 0]]
To elaborate on the great hint provided by njzk2:
Turn your list of lists into a list of tuples
Create a Counter from it
Get a dict from the Counter
Set the 3rd element of the sublists to the frequency from the Counter
from collections import Counter
lst = [[1, 0, 0, 0], [1, 1, 0, 0], [2, 0, 0, 0], [2, 1, 0, 0], [2, 1, 0, 0], [1, 1, 0, 0], [3, 1, 0, 0], [1, 3, 0, 0], [2, 1, 0, 0], [2, 0, 0, 0]]
list_of_tuples = [tuple(elem) for elem in lst]
dct = dict(Counter(list_of_tuples))
lst = [list(e) for e in dct]
for elem in lst:
elem[2] = dct[tuple(elem)]
Edit: removed duplicates with the line before the for loop. Didn't see that requirement before.
You can do this to keep count of the duplicates:
lst = [[1, 0], [1, 1], [2, 0], [2, 1], [2, 1], [1, 1], [3, 1], [1, 3], [2, 1], [2, 0]]
for x in lst:
count = 1
tmpLst = list(lst)
tmpLst.remove(x)
for y in tmpLst:
if x[0] == y[0] and x[1] == y[1]:
count = count + 1
x.append(count)
#x.append(0) #if you want to add that 4th element
print lst
Result:
[[1, 0, 1], [1, 1, 2], [2, 0, 2], [2, 1, 3], [2, 1, 3], [1, 1, 2], [3, 1, 1], [1, 3, 1], [2, 1, 3], [2, 0, 2]]
Then you can take lst and remove duplicates as mentioned in the link you posted.
A different (maybe functional) approach.
lst = [[1, 0, 0, 0], [1, 1, 0, 0], [2, 0, 0, 0], [2, 1, 0, 0],\
[2, 1, 0, 0], [1, 1, 0, 0], [3, 1, 0, 0], [1, 3, 0, 0],\
[2, 1, 0, 0], [2, 0, 0, 0]]
def rec_counter(lst):
# Inner method that is called at the end. Receives a
# list, the current element to be compared and an accumulator
# that will contain the result.
def counter(lst, elem, acc):
new_lst = [x for x in lst if x != elem]
elem[2] = lst.count(elem)
acc.append(elem)
if len(new_lst) == 0:
return acc
else:
return counter(new_lst, new_lst[0], acc)
# This part starts the recursion of the inner method. If the list
# is empty, nothing to do. Otherwise, count starting with the first
# element of the list and an empty accumulator.
if len(lst) == 0:
return []
else:
return counter(lst, lst[0], [])
print rec_counter(lst)
# [[1, 0, 1, 0], [1, 1, 2, 0], [2, 0, 2, 0], \
# [2, 1, 3, 0], [3, 1, 1, 0], [1, 3, 1, 0]]

Categories