This question already has answers here:
Python: See if one set contains another entirely? [duplicate]
(7 answers)
Closed 2 years ago.
I have 2 elements for example 1 and 2.
I need to find out if both of these elements are in my array.
For example:
I have 1,2,3,4. And i have Array [2,3,4,111].
If all my elements are in the array, then it's True, else False
array = [2,3,4,111]
elements_to_check = [2,3]
result = set(elements_to_check).issubset(array)
print(result)
Prints True if elements_to_check = [2,3]
Prints False if elements_to_check e.g. = [2,3,5]
Another solution would be
list = [2,3,4,111]
elements_to_check = [1,2]
result = len([x for x in elements_to_check if x in list]) == len(elements_to_check)
Prints True if elements_to_check = [2,3]
Prints False if elements_to_check e.g. = [1,2]
Related
This question already has answers here:
Fastest way to check if a value exists in a list
(11 answers)
Closed 1 year ago.
I have a long list of values and want a list comprehension to evaluate to True (and print "True" only once if any value in the list is the integer 1).
I can print "True" for each instance a 1 is found but cannot see how to just have it return a single True.
Code
a = [0,0,1,1,0,1]
b = [print("True") for i in a if i == 1]
print('\n')
#c = [print("True") if any i in a is True] # doesn't work, syntax error
d = [print("TRUE") if any(i == 1)]
Did you mean to convert the resulting list to bool()?
a = [0,0,1,1,0,1]
b = bool([i for i in a if i == 1])
print(b)
if your list only contains zeros and ones you could just print(any(a))
otherwise you could do this
a = [0,0,1,0,2,0]
b =[x==1 for x in a]
print(any(b))
returns True
This question already has answers here:
How Pytorch Tensor get the index of specific value
(10 answers)
Closed 3 years ago.
I have 2 Tensors named x and list and their definitions are below:
x = torch.tensor(3)
list = torch.tensor([1,2,3,4,5])
Now I want to get the index of element x from list. The expected output is an Integer:
2
How can I do in an easy way?
import torch
x = torch.tensor(3)
list = torch.tensor([1,2,3,4,5])
idx = (list == x).nonzero().flatten()
print (idx.tolist()) # [2]
list = torch.tensor([1,2,3,3,5])
idx = (list == x).nonzero().flatten()
print (idx.tolist()) # [2, 3]
This question already has answers here:
Finding if any element in a list is in another list and return the first element found
(6 answers)
Closed 4 years ago.
I want to check if all the numbers inside "check" is inside a given array "lists", something like this:
import sys
lists = [1,2,8,8,8,8,8,8,8,8,8,8]
check = [1,2,3]
for check in lists:
print True
sys.exit()
print False
However, this prints True all the time except if I make lists = []. Does any one know how this will work? Thanks in advance!
Updated working
s = [True if lists[idx: idx+3] == check else False for idx, item in enumerate(lists[:-2])]
if any(s):
print(True)
else:
print(False)
it's keep printing True because you're printing True inside the loop, and if the list is [], it can't iterate because it's empty, so try just:
print(all(i in lists for i in check))
So whole code is:
lists = [1,2,8,8,8,8,8,8,8,8,8,8]
check=[1,2,3]
print(all(i in lists for i in check))
Update:
print(all(i in lists for i in check) and [lists.index(i) for i in check]==range([lists.index(i) for i in check][0],[lists.index(i) for i in check][-1]+1))
This question already has answers here:
Understanding slicing
(38 answers)
Closed 4 years ago.
Say I have a list arr and an index i within [0:len(arr)], how do I get all the elements starting from arr[i] up to arr[-1] in a Pythonic way?
For example:
arr = 'abcdef'
i = 3
I basically want to get b = 'def'
I tried
b = a[i:-1]
but obviously that'll leave out the last element.
Also, my list sometimes has only 1 element, so i = 0. How do I safely treat that edge case?
Thank you!
You could use python list slicing like this:
b = arr[i:] # This prints 'def' when arr = 'abcdef' and i = 3
This will print everything from the ith position to the end. And if i is greater than the string length, it'll print an empty string.
This question already has answers here:
Comparing two lists using the greater than or less than operator
(3 answers)
Closed 5 years ago.
I want to compare two lists. For example:
a = [8,9,9,11]
b = [8,7,20,10]
if a >= b :
print "true"
Why does this print "true"? I want to compare the values vertically like this:
8 >= 8 is true
9 >= 7 is true
9 >= 20 is false but the program return true
11 >= 10 is true
You can use list comprehension and all function as follows:
code:
a = 8,9,9,11
b = 8,7,20,10
print all([(a > b) for a, b in zip(a,b)])
output:
False
You can use a list comprehension for comparing two lists element wise then use all function to check all of comparison are True:
a = [8,9,9,11]
b = [8,7,20,10]
all(a[i]>=b[i] for i in range(len(a))) # False