Related
Is it possible somehow to preform logical functions on arrays like eg.
a= [1,2,3,4,5,6]
b= [1,3,5,7]
c= a and b
resulting in c=[1,3,5]
so only the values that are present in both the arrays.
The same with or eg:
d = a OR b
resulting in b=[1,2,3,4,5,6,7]
Is this possible somehow in python or are there short functions for this?
Thanks for your response
Lists do not support logical operations , but you can do it in sets:
a= [1,2,3,4,5,6]
b= [1,3,5,7]
c = list(set(a) | set(b))
d = list(set(a) & set(b))
print(c)
# OUTPUT: [1, 2, 3, 4, 5, 6, 7]
print(d)
# OUTPUT: [1, 3, 5]
You probably should use python set if your collections contain unique values. They are actually mathematical sets: https://en.wikipedia.org/wiki/Set_(mathematics) and support general operation for sets such as intersection and union
>>> a = {1, 2, 3, 4, 5, 6}
>>> b = {1, 3, 5, 7}
>>> c = a.intersection(b)
>>> c
{1, 3, 5}
Here is your snippet:
def and_array(a, b):
return [a[i] for i in range(min(len(a), len(b))) if a[i] in b or b[i] in a]
def or_array(a, b):
return a + [element for element in b if not element in a]
a= [1,2,3,4,5,6]
b= [1,3,5,7]
c = and_array(a, b)
d = or_array(a, b)
print(c)
print(d)
Result:
[1, 2, 3]
[1, 2, 3, 4, 5, 6, 7]
It is not that fast, avoid this for very large lists and use numpy or build-in functions!
I want to take multiple lists and know the values that appaer in these lists with the possibility of one or more of the lists being empty.
CASE 1:
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]
c = []
d = [2, 3, 5]
Needed output: 5
CASE 2:
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5, 3]
c = [1, 3, 8]
d = []
Needed output: 3
Order doesn't matter.
I know I can use this if there are no empty lists (source 1 / source 2)
list(set(a) & set(b) & set(c) and set(d))
The problem occurs when one of the lists is empty.
Python version: 3.8
Lets use a list comprehension with set.intersection -
The list comprehension checks if the length of the list is greater than 0 and then uses it to find an intersection using set.intersection. The * operator unpacks this list of sets, to individual sets.
Case 1:
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]
c = []
d = [2, 3, 5]
l = [a,b,c,d]
l = [set(i) for i in l if len(i)>0]
set.intersection(*l)
{5}
Case 2:
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5, 3]
c = [1, 3, 8]
d = []
l = [a,b,c,d]
l = [set(i) for i in l if len(i)>0]
set.intersection(*l)
{3}
You use filter() to remove empty lists and then convert your list to set() objects using map() and do the intersection as:
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]
c = []
d = [2, 3, 5]
my_list = [a, b, c, d]
my_result = set.intersection(*map(set, filter(bool, my_list)))
# where `my_result` holds the value:
# {5}
Which is the most pythonic way and the fastest way (could be the same) to append many list together? For example, given the lists below:
a = [1, 2]
b = [3, 4]
c = [5, 6]
d = [7, 8]
we get one list:
combined = [1, 2, 3, 4, 5, 6, 7, 8]
In Python 3.5+, you can use the generic unpacking:
combined = [*a, *b, *c, *d]
or prior to Python 3.5+, you can use itertools.chain:
from itertools import chain
combined = list(chain(a, b, c, d))
I can't understand you
do you mean how to merge them?
for example
a = [1, 2]
b = [3, 4]
c = [5, 6]
d = [7, 8]
combined = a + b + c + d
so combined will be
[1, 2, 3, 4, 5, 6, 7, 8]
OR:
a = [1, 2]
b = [3, 4]
c = [5, 6]
d = [7, 8]
a.extend(b)
a.extend(c)
a.extend(d)
Now:
print(a)
Returns:
[1, 2, 3, 4, 5, 6, 7, 8]
I checked similar problem at sum of N lists element-wise python
but mine is a little bit more complicate cause I am adding list of list with another list of list
my list looks like it below
[[0 0.014285714 0.035600016]
,[0.014285714 0 0.038359389]
,[0.035600016 0.038359389 0]]
[[0 0.014285714 0.035600016]
,[0.014285714 0 0.038359389]
,[0.035600016 0.038359389 0]]
so adding these two then results has 3 by 3 matrix
how could I efficiently add these two lists?
Can i solve using numpy:
>>> a = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> b = [[4, 5, 6], [4, 5, 6], [4, 5, 6]]
>>> from numpy import array, sum
>>> list(array(a) + array(b))
[array([5, 7, 9]), array([5, 7, 9]), array([5, 7, 9])]
OR
>>> sum([a, b], axis=0)
array([[5, 7, 9],
[5, 7, 9],
[5, 7, 9]])
>>>
You tagged your question numpy, so I assume this is OK:
import numpy as np
a = np.matrix(a)
b = np.matrix(b)
c = a + b
Where a and b are your two lists, and c is the two added together.
By far the simplest with numpy. :)
If you are working with numpy matrices, #AlexThornton has the best answer
If you are dealing with lists, you should do:
summed_list = [[c+d for c,d in zip(i,j)] for i,j in zip(a,b)] #a,b are your two lists
Simple example:
>>> a = [[1,2,3],[1,2,3],[1,2,3]]
>>> b = [[4,5,6],[4,5,6],[4,5,6]]
>>> summed_list = [[c+d for c,d in zip(i,j)] for i,j in zip(a,b)] #a,b are your two lists
>>> print summed_list
[[5,7,9],
[5,7,9],
[5,7,9]]
Here is a recursive solution using 'vanilla' python and recursion
(This solution works for nested tuples or lists)
from operator import add
def list_recur(l1, l2, op = operator.add):
if not l1:
return type(l1)([])
elif isinstance(l1[0], type(l1)):
return type(l1)([list_recur(l1[0], l2[0], op)]) + \
list_recur(l1[1:],l2[1:], op)
else:
return type(l1)([op(l1[0], l2[0])]) + \
list_recur(l1[1:], l2[1:], op)
This solution takes arguments list1, list2 and optionally the operation you want to perform element-wise (by default addition).
For example, list_recur([ [1, 2, 3, [4, 5] ], [1, 2, 3, [4, 5] ]) will return [ [2, 4, 6, [8, 10] ].
More complex examples will work too:
list_recur([[[[[1,[[[[2, 3]]]]]]]], [[[[3, [[[[4, 1]]]]]]]], lambda x, y: \
x < y) will return [[[[True, [[[[True, False]]]]]]]]
`
I'd like to select unique value in many lists, but I don't know how to do that:
a = [1,2,3,4,5]
b = [2,3,4,5,6]
c = [5,6,7,8,9]
I'd like to make one new list which is [1,2,3,4,5,6,7,8,9]
I know that using the following could be done, but I am searching quicker way to do that.
for i in (a, b, c):
for j in EachValueInEachList:
NewList.append(j)
list(set(NewList)
By the way, there are thousand of lists in my real program.
Thank you very much.
>>> a = [1,2,3,4,5]
>>> b = [2,3,4,5,6]
>>> c = [5,6,7,8,9]
>>> list(set(a + b + c))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
To avoid creating temporary list, use itertools.chain:
>>> import itertools
>>> list(set(itertools.chain(a, b, c)))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
UPDATE (answer to the comment)
If you have a list of lists, use itertools.chain.from_iterable:
list(set(itertools.chain.from_iterable(a_list_of_lists)))
>>> list(set(a) | set(b) | set(c))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
numpy is best option if long list.
import numpy as np
import time
start_time = time.time()
a = np.array([[1,2,3,4,5], [2,3,4,5,6],[5,6,7,8,9]])
print np.unique(a).tolist()
print time.time() - start_time # Execution time
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
0.000999927520752