This question already has answers here:
Best way to find the intersection of multiple sets?
(7 answers)
Closed 3 years ago.
list(set(a[0]) & set(a[1]) & set(a[2]) & set(a[3]) & set(a[4]))
Does anyone know how to write this in a way such that we dont need to know apriori how many lists we will be given? (ie 5 not hard coded in)?
Each a is a list of varying size.
As long as you have at least one set, you can do this:
list(set(a[0]).intersection(*a[1:]))
If there might be no sets, you have to decide for yourself what the "intersection of no sets" actually should mean in your application. If you want the empty set:
list(set(*a[:1]).intersection(*a[1:]))
I think it's worth noting, at least to improve one's general programming understanding, that what you want to do can be described as mapping and then reducing or folding. Specifically, you want to map set over a and then fold & over the result.
I'm not a Python expert, but it can be done like this in Python:
from functools import reduce
a = [
[1,2,3],
[1,2,3,4],
[1,2,4,5],
[1,2,3,5],
]
intersection = lambda x, y: x & y
mapped = list(map(set, a))
reduced = reduce(intersection, mapped)
Note that this implementation requires a to be non-empty.
Related
This question already has answers here:
How can I find same values in a list and group together a new list?
(6 answers)
Closed 2 years ago.
I have a list like so:
[10,10,10,20,20,20,20,30,40,40,40]
I want to split into X amount of lists, where X = how many unique elements there are, in the case above there are 4. So I would want 4 lists like so:
[[10,10,10],[20,20,20,20],[30],[40,40,40]]
Might be a dumb question and there is an easy way to do this but any help is appreciated, language is python3.
itertools.groupby does what you need, except it returns iterators instead of lists. Converting to lists is easy though:
[list(g) for _, g in itertools.groupby(my_list)]
This question already has answers here:
How do Python's any and all functions work?
(10 answers)
Closed 3 years ago.
Just thought I would specify in the title, because I have found the latter all over the internet, but not the former. I am curious how to get a conditional that contains a list comprehension working in python. Specifically, I am curious about how to do something like the following:
if (abs(value - any_x)) > 100 for any_x in x:
Essentially, I want the program to proceed if the absolute value of the difference between the value and any value in the x array is greater than 100. But the syntax as it stands is incorrect. What exactly am I missing? Thanks and best regards,
-AA
Use any:
if any(abs(value - any_x) > 100 for any_x in x):
...
Don't use a list comprehension here as any will return True on the first True value it finds. Thus providing it a generator is the most efficient method as it will be lazily evaluated.
You can use any.
if any(abs(value - any_x) > 100 for any_x in x):
Pretty simple,
True in [abs(k-value)>100 for k in x]
This question already has an answer here:
Numpy find indices of groups with same value
(1 answer)
Closed 3 years ago.
I have a boolian vector
v=[True,True,True,False,True,True,False,False,True,True,False,True]
I want to get the intervals that contain the True values.
#intervals in the format: [start,stop]
[[0,2],
[4,5],
[8,9],
[11,11]]
Is there any commonly used function in python (let's say in numpy) that can do this.
Not necessarily the fastest but probably one of the shortest. The new prepend and append keywords to np.diff are very convenient for this kind of task.
np.flatnonzero(np.diff(v, prepend=False, append=False)).reshape(-1, 2) - (0, 1)
This question already has answers here:
Check if all elements in a list are identical
(30 answers)
Closed 8 years ago.
I am trying to compare elements of a list u for equality.
A possible solution could be all(x == u[0] for x in u[1:]), or simply all(x == u[0] for x in u), but it looks rather weird.
In Python, it's possible to write a == b == c, with its usual "mathematical" meaning. So I thought I could, with the help of the operator module, write operator.eq(*u). However, the eq function takes only two arguments. Of course, functools.reduce(operator.eq, u) is of no use here, since after the first test eq(u[0], u[1]), I get a boolean, and it will fail when doing the second test, eq(<bool>, u[2]).
Is there a better way than the solution above? A more... "pythonic" way?
len(set(u)) == 1 is pretty Pythonic.
This question already has answers here:
How can I multiply all items in a list together with Python?
(15 answers)
Closed 5 months ago.
I note this question has already been asked here, but this mostly deals with python2:
How can I multiply all items in a list together with Python?
With the demise of reduce in python3 (see What is the problem with reduce()?), what is the best way to multiply numbers in an iterable together?
eg. [1,3,7,1,2] -> 1*3*7*1*2
I'm using something like this at the moment
def foo(list)
sum = 1
for i in list:
sum *= i
return sum
I'd really like a one liner, without having to from functools import reduce
Something like: total = sum(b for a,b in items)
but for multiplication
The major objection to reduce seems to be abusing it with arbitrary reduction functions. If you stick with simple, pre-existing associative operators, there's no reason not to use reduce.
from functools import reduce
from operator import mul
x = reduce(mul, [1,3,7,1,2])
You can even go one step further and compose reduce and mul using functools.partial.
product = functools.partial(functools.reduce, operator.mul)
x = product(b for a, b in items)