How can I check if a string of list2 is insinde a string of a list of list1?
For example:
list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]
A simple solution using loops
list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]
for x in range(len(list1)):
for y in range(len(list2)):
if str(list1[x]).find(str(list2[y])) == -1 :
print("NO")
else :
print("YES")
Function returns true if element of list2 exists in list1
def my_find(list1, list2):
for cur in list2:
for cur_list in list1:
if cur in cur_list:
return True
return False
here you go:
list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]
res = [[l in e[0] for l in list2] for e in list1]
Related
Just to know if it is possible to write this in a line comprehension and if it is, how to write it.
lst1 = [0,1,2,3,4,5,6,7,8,9,10]
lst2 = [[0,4],[1,5],[2,6],[3,7]]
list3 = []
list4 = []
list5 = []
for l in lst1:
for k in lst2:
if l == k[0]:
list3.append(k)
elif l==k[1]:
list4.append(k)
else:
list5.append(l)
print('lst1',lst1)
print('lst2',lst2)
print('list3',list3)
print('list4',list4)
print('list5',list5)
You can generally turn a nested for loop that builds a list into a list comprehension by using the same exact for ... in statements in the same order:
list3 = [k for l in lst1 for k in lst2 if l == k[0]]
list4 = [k for l in lst1 for k in lst2 if l == k[1]]
list5 = [l for l in lst1 for k in lst2 if l not in k]
I have two lists and I want to check if my list of strings contains all the elements of the second list.
I would like it to return True only if all the elements in list2 are in substrings of elements in list1 (True in this example).
Here is my code :
list1 = ['banana.two', 'apple.three', 'raspberry.six']
list2 = ['two', 'three']
if all(elem in elem in list1.tolist() for elem in list2):
Try this:
list1 = ['banana.two', 'apple.three', 'raspberry.six']
list2 = ['two', 'three']
def check(strings, substrings):
for substring in substrings:
if not (any(substring in string for string in strings)):
return False
return True
print(check(list1, list2))
You were close. You need to combine all and any according to this: check that all strings in list2 appear in any string in list1. Directly translates to:
list1 = ['banana.two', 'apple.three', 'raspberry.six']
list2 = ['two', 'three']
if all(any(sub in string for string in list1) for sub in list2):
print("Success!")
substring = [i for e in list2 for i in list1 if e in i]
if len(susbstring) != 0:
return True
else:
return False
Having two lists:
list1 = ['Me','You','Sam']
list2 = ['Joe','Jen']
How can I append items of list2 to the list1?
Expected result is:
['Me', 'You','Sam','Joe','Jen']
My failed attempt:
list1 = ("Bobs","Sams","Jacks");
foreach list2 in list1:
list3 = list2 + " list item"
print list3
This should get you started:
list1 = ['Me','You','Sam']
list2 = ['Joe','Jen']
for item in list2:
list1.append(item)
list1 now is ['Me', 'You','Sam','Joe','Jen']
If you want a third list, simply define it and append to it instead.
So a couple of things are wrong with your code.
First:
list = ("Bobs", "Sams", "Jacks"); should be list = ["Bobs", "Sams", "Jacks"]
Second:
foreach list2 in list1:
list3 = list2 + " list item"
should be
list3 = []
for item in list1:
list3.append(item)
I need help to search a value in a range inside a list
list.append((5,range(10,15)))
if (5,12) in list :
Print("yes")
Use all
Ex:
l = (5,range(10,15))
check = (5,12)
if all([check[0] == l[0] and check[1] in l[1]]):
print("Ok")
Output:
Ok
Make the second range as a list and you can do it this way-
result = any(elem in list1 for elem in list2)
where result is True or False
Perhaps you're looking for something like this?
mylist = []
mylist.extend(list(range(10, 15)) + [5])
check_list = [5, 12]
if all([z in mylist for z in check_list]):
print('yes')
Output:
yes
Try this :
list_1 = []
for i in range(10,15):
list_1.append((5,i))
if (5,12) in list_1:
print("yes")
If list_1 not an existing list you can do like this:
list_1 = [(5,i) for i in range(10,15)]
Having two lists:
list1 = ['Me','You','Sam']
list2 = ['Joe','Jen']
How can I append items of list2 to the list1?
Expected result is:
['Me', 'You','Sam','Joe','Jen']
My failed attempt:
list1 = ("Bobs","Sams","Jacks");
foreach list2 in list1:
list3 = list2 + " list item"
print list3
This should get you started:
list1 = ['Me','You','Sam']
list2 = ['Joe','Jen']
for item in list2:
list1.append(item)
list1 now is ['Me', 'You','Sam','Joe','Jen']
If you want a third list, simply define it and append to it instead.
So a couple of things are wrong with your code.
First:
list = ("Bobs", "Sams", "Jacks"); should be list = ["Bobs", "Sams", "Jacks"]
Second:
foreach list2 in list1:
list3 = list2 + " list item"
should be
list3 = []
for item in list1:
list3.append(item)