How to slice lists in Python based on the length of its elements
By : Suresh Nookala
Date : March 29 2020, 07:55 AM
To fix this issue How to slice a list based on the length of its elements? For example, how do I turn , You can try this: code :
l = ['A', 'E', 'LA', 'ELA', 'B', 'CD']
maximum = max([len(i) for i in l])
minimum = min([len(i) for i in l])
l = list([i for i in l if len(i)==s] for s in range(minimum, maximum+1))
print(l)
[['A', 'E', 'B'], ['LA', 'CD'], ['ELA']]
|
Merge Two Scala Lists of Same Length, With Elements At Same Index Becoming One Element
By : Jamela314
Date : March 29 2020, 07:55 AM
it helps some times I have two Scala lists with the same number and type of elements, like so: , Combine zip and map: code :
x zip y map { case (a, b) => a + b }
x zip y map (_.productIterator.mkString)
|
How can I compare elements in 2 lists in the same index in their respective lists in Python?
By : user3444400
Date : March 29 2020, 07:55 AM
may help you . Say I have 2 lists , simplest method would probably just be doing a list comprehension: code :
c = [x for x,y in zip(a,b) if x == y]
from itertools import compress
mask = [x==y for x,y in zip(a,b)]
c = list(compress(a,mask))
import numpy as np
a,b = np.array(a), np.array(b)
c = a[np.equal(a,b)].tolist()
[1, 2, 7, 1]
|
Python creating a new list that contains elements not equal to elements of the same index of two other lists
By : virtualyyours
Date : March 29 2020, 07:55 AM
Hope this helps I am trying to create a list by iterating through a two other lists using a list comprehension and only leaving integers in the list that are not equal to the same index element of the other two lists. , You could do something like this: code :
import random
random.seed(42)
list_1 = [0,3,2,5,7,2,3,5,9,2]
list_2 = [1,7,2,5,0,0,2,3,0,4]
n = 10
pool = set(range(n))
result = [random.sample(pool - set(t), 1)[0] for t in zip(list_1, list_2)]
print(result)
[3, 0, 5, 3, 4, 4, 1, 1, 7, 0]
result = [random.choice(list(pool - set(t))) for t in zip(list_1, list_2)]
|
In python, how to compare two lists: same length and could have duplicate elements
By : unknow
Date : September 18 2020, 12:00 AM
will help you Say I have a = [1,1,3,4] and b = [3,1,4,1] and I want a results matched. If different length or some elements are different, then return unmatched , If you don't care about the order of elements you can do this: code :
def compare(a, b):
return 'matched' if sorted(a) == sorted(b) else 'unmatched'
a = [1,1,3,4]
b = [3,1,4,1]
print(compare(a, b))
>>> 'matched'
|