How do I check whether comma separated elements of a string are in the list or not?
Question:
a = "3"
b = ["3"]
def func():
return a in b
The above function returns "TRUE"
But if I have my code as follows:
a="1,2,3,4"
b = ["3"]
How do I check the elements of a one by one with b, i.e, "1"==["3"] or "2"==["3"], and so on, and return "TRUE" when "3"==["3"]
Answers:
Try this
a="1,2,3,4"
b = ["3"]
def func(a,b):
return any(e in b for e in a.split(','))
print(func(a,b))
Output
True
- Use
split(',')
for converting string to list.
- Code inside the
any()
function e in b for e in a.split(',')
returns a list of True and False based on condition, Here e
values are 1
, 2
, 3
, 4
one by one and check if e
is in b
list.
- Use the
any()
function, It returns True if one of the conditions is True in the list.
It’s simple.Try this:
a="1,2,3,4"
b = ["3"]
a_list = a.split(',')
for i in a_list:
if i in b:
print("True")
break
Output:
True
I guess you’re using Python. You can use the Python ‘split’ method on ‘a’ to create a new list that contains elements separated by ‘,’, like this:
new_list=a.split(',')
Now you can use a for loop to go through ‘new_list’ and check if any element equals ‘b’.
def get_list_from_file(filename):
with open(filename, 'r') as f:
contents = f.read()
return contents.split(',')
def search_list(contents, value):
if value in contents:
return True
else:
return False
contents = get_list_from_file('test.csv')
value = input('Enter a value to search for: ')
if search_list(contents, value):
print('Found')
else:
print('Not found')
-
You can use the split method to convert string to array
-
Compare your value with the array.
arr = "1,2,3,4"
b = ["3"]
def find():
return b[0] in x
x = arr.split(‘,’)
x = find()
print(x)
a = "3"
b = ["3"]
def func():
return a in b
The above function returns "TRUE"
But if I have my code as follows:
a="1,2,3,4"
b = ["3"]
How do I check the elements of a one by one with b, i.e, "1"==["3"] or "2"==["3"], and so on, and return "TRUE" when "3"==["3"]
Try this
a="1,2,3,4"
b = ["3"]
def func(a,b):
return any(e in b for e in a.split(','))
print(func(a,b))
Output
True
- Use
split(',')
for converting string to list. - Code inside the
any()
functione in b for e in a.split(',')
returns a list of True and False based on condition, Heree
values are1
,2
,3
,4
one by one and check ife
is inb
list. - Use the
any()
function, It returns True if one of the conditions is True in the list.
It’s simple.Try this:
a="1,2,3,4"
b = ["3"]
a_list = a.split(',')
for i in a_list:
if i in b:
print("True")
break
Output:
True
I guess you’re using Python. You can use the Python ‘split’ method on ‘a’ to create a new list that contains elements separated by ‘,’, like this:
new_list=a.split(',')
Now you can use a for loop to go through ‘new_list’ and check if any element equals ‘b’.
def get_list_from_file(filename):
with open(filename, 'r') as f:
contents = f.read()
return contents.split(',')
def search_list(contents, value):
if value in contents:
return True
else:
return False
contents = get_list_from_file('test.csv')
value = input('Enter a value to search for: ')
if search_list(contents, value):
print('Found')
else:
print('Not found')
-
You can use the split method to convert string to array
-
Compare your value with the array.
arr = "1,2,3,4"
b = ["3"]def find():
return b[0] in xx = arr.split(‘,’)
x = find()print(x)