Reputation: 13
I am fairly new to python and I am trying to figure out how to find if the elements of a list equal a given string?
lists=["a","b",'c']
str1='abc'
I know it is probably easy, but I am having a hard time without using string methods.
Thanks, DD
Upvotes: 1
Views: 98
Reputation: 7931
you can use .join to create a string from your list:
list = ['a', 'b', 'c']
strToComapre = ''.join(list1)
Now you can check if strToComapre is "in" the original str:
if strToCompare in originalStr:
print "yes!"
If you want a pure compare use:
if strToCompare == originalStr:
print "yes! it's pure!"
There are lots of options in python i'll add some other useful posts:
Compare string with all values in array
http://www.decalage.info/en/python/print_list
Upvotes: 0
Reputation: 251146
>>> lists=["a","b",'c']
>>> str1='abc'
>>> ''.join(lists) == str1
True
Upvotes: 1
Reputation: 474191
>>> l = ['a', 'b', 'c']
>>> l == list('abc')
True
But, if the order of items in the list can be arbitrary, you can use sets:
>>> l = ['c', 'b', 'a']
>>> set(l) == set('abc')
True
or:
>>> l = ['c', 'b', 'a']
>>> s = set(l)
>>> all(c in s for c in 'abc')
True
Upvotes: 2