This question already has answers here:
Python -Intersection of multiple lists?
(6 answers)
Closed 4 years ago.
Is it possible to compare a list containing an unknown number of lists with equal elements in a more terse (aka shorter) manner than what I have done? Preferably an one-liner!
Here's an example if it's unclear what I want to do:
a = [1, 2, 3]
b = [4, 2, 1]
c = [7, 5, 1]
d = [a, b, c]
def multiCompList(lists):
final = [i for i in lists[0] if i in lists[1]]
for i in range(2, len(lists)):
final = [i for i in final if i in lists[i]]
return final
print(multiCompList(d))
What I've done is to first check if the first and second list contains any equal elements and put them in a list called final. Thereafter, checking if those elements can be found in the lists after and replacing the final-list with the remaining equal elements.
The results in this case is: [1].
A oneliner would look like this:
set.intersection(*[set(x) for x in d])
#set([1])
Related
This question already has answers here:
How to find the cumulative sum of numbers in a list?
(25 answers)
Closed last year.
Consider the list:
a = [1,2,3,4,5]
How can I iterate through this list summing the consecutive elements and then producing a new list as follows:
b = [1,3,6,10,15]
such that each element in list b is the sum of all the prior elements up to that index number from list a. i.e. b[0] = a[0], b[1] = a[0]+a[1], b[2] = a[0]+a[1)+a[2] etc.
This is merely pseudo-code. In reality, my real list 'a' has thousands of elements, so i need to automate; not just write b[1] = a[0] + a[1] and do that for the 5 elements of b. Any ideas would be greatly appreciated: I am new to python.
You could use list comprehension as follow:
a = [1,2,3,4,5]
b = [sum(a[:i+1]) for i in range(len(a))]
If you're willing to use numpy this is just the cumsum function:
import numpy as np
a = [1,2,3,4,5]
np.cumsum(a)
# array([ 1, 3, 6, 10, 15], dtype=int32)
This question already has answers here:
What does the built-in function sum do with sum(list, [])?
(3 answers)
Closed 3 years ago.
I have recently discovered an interesting feature in python
If you type:
y=[[1,2],[3,4]]
sum(y,[])
Output is: [1, 2, 3, 4]
Does anyone know why the sum of a series of lists with an empty list gives a flattened version of y (i.e: all of the sub-lists of y as a single list)?
I would have expected the output to be a concatenation:
[1,2],[3,4],[]
Thanks
sum iterates through an iterable and adds each element. It just so happens that with lists, addition is defined to be concatenation. By setting the second parameter (start) to [], the function begins with a list and keeps adding on elements to it. Effectively:
sum([[1, 2], [3, 4]], []) == [] + [1, 2] + [3, 4]
This question already has answers here:
List comprehension vs. lambda + filter
(17 answers)
Closed 3 years ago.
I want to write code that returns if a list has values bigger than
any number for example 4, but none that returns me only the values that go till 4 as a sublist.
I've already tried all the comparison operators, to make sure I'm using the right one, countless of different codes, etc.
if len(list) == 1:
return list[0]
else:
return list[0] + sublist(list[1:])
another try:
if len(list) <4:
return list[0]
the perfect output would be for example:
for:
sublist([1, 2, 3, 4, 7, 8])
it should return :
[1, 2, 3, 4]
Use a list comprehension.
return [x for x in mylist if x <= 4]
This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
How to remove items from a list while iterating?
(25 answers)
Closed 4 years ago.
I'm trying to iterate over a list of number and removing the values that are lower than a number that I use to compare.
My problem is that there's a number that is lower than the value that I use but it doesnt get removed.
I'm using the remove() function of the list but I don't know why it doesn't get removed
Here is my code:
def remove_lower_numbers(array_numbers, bigger_number):
for elem in array_numbers:
if elem <= bigger_number:
array_numbers.remove(elem)
print(array_numbers)
It works if I used a list comprehension like this:
array_numbers = [x for x in array_numbers if x >= bigger_number]
but I want to do it the way I firts mentioned for learning purposes
I call the function like this:
cards_array = [3, 2, 7]
remove_lower_numbers(cards_array, 8)
but the function prints:
[2]
and 2 is lower than 8 it should return None or a empty list.
Using filter, which keeps only the values that return True for the lambda function:
list(filter(lambda x: x > 3, [1, 2, 3, 4, 5, 2, 3]))
Output:
[4, 5]
This question already has answers here:
Remove all the elements that occur in one list from another
(13 answers)
Closed 6 years ago.
I'm stuck with a part in one of my codes where I have to delete all the occurances present in listA that are identical in listB.
Example:
A=[1,4,4,4,3,3,2,1,5,5]
B=[4,3]
Result should be A=[1,2,1,5,5]. Ideally I would want to do it in linear time.
using Set Operations:
list(set(A) - set(B))
Using List Comprehension
list(set([i for i in A if i not in B]))
Try with list comprehension,
In [11]: [i for i in A if i not in B]
Out[11]: [1, 2, 1, 5, 5]