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
>>> 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
>>> lists=["a","b",'c']
>>> str1='abc'
>>> ''.join(lists) == str1
True
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
Related
To filter list of strings by another list of strings in Python we can use the following code:
result = [x for x in strings1 if x in strings2]
But how can we filter list of substrings by another list of strings? For example:
substrings = ['a', 'b', 'c']
strings = ['_b_', '_c_', '_d_']
Result should be:
result = ['b', 'c']
You can use something like that:
[x for x in substrings if [y for y in strings if x in y]]
In [1]: substrings = ['a', 'b', 'c']
In [2]: strings = ['_b_', '_c_', '_d_']
In [3]: [x for x in substrings if [y for y in strings if x in y]]
Out[3]: ['b', 'c']
Elegant way to achieve this is via using any with a list comprehension as:
>>> substrings = ['a', 'b', 'c']
>>> my_strings = ['_b_', '_c_', '_d_']
>>> [s for s in substrings if any(s in i for i in my_strings)]
['b', 'c']
Here any will return True if any of string in substrings is present as substring in my_strings. As soon as it finds the match, it will short-circuit the iteration (without checking for other matches) and will return the result as True. Due to the short-circuiting property of any, it won't make the unnecessary iteration across the entire list, resulting in better performance.
I have two lists:
input_list = ['a','b']
existing_list = ['q','w','r']
I want to create a new list that will be equal to input_list if it's not empty else it should be equal to existing_list.
The easiest way to do this is to use the plain if-else:
if input_list:
list_to_use = input_list
else:
list_to_use = existing_list
Is it possible to do this in a list comprehension or in any other manner that is more concise?
list_to_use = [x if input_list else y for y in existing_list for x in input_list]
The closest I could get is with this one that produces ['a', 'b', 'a', 'b', 'a', 'b'], a wrong result.
You don't need a list comprehension. That's exactly what or operation does:
>>> input_list or existing_list
['a', 'b']
>>> input_list = []
>>>
>>> input_list or existing_list
['q', 'w', 'r']
Other than or, that suggested by Kasramvd (which should be the way to do this), you can also use ternary operator.
list_to_use = input_list if input_list else existing_list
I have a list of strings and variables. For example:
['oz_', A, 'ab'], where A is a list and I don't want anything to happen to it.
And I want to convert it in:
['o','z','_', A, 'a', 'b']
A is a list, so I don't want anything to change it. How can I do this?
You'll need to iterate over each element and turn it into a list if it's a string, but otherwise leave it as a variable and append it.
source = ['oz_', A, 'ab']
result = []
for name in source:
if isinstance(name, str):
result += name
else:
result.append(name)
Note: Use isinstance(name, basetring) for Python2.x if you want to account for other types of string like unicode.
Updated now that we know A shall not be altered.
A = []
seq = ['oz_', A, 'ab']
res = []
for elt in seq:
if isinstance(elt, str):
for e in list(elt):
res.append(e)
else:
res.append(elt)
print(res)
output:
['o', 'z', '_', [], 'a', 'b']
Obligatory one-liner:
>>> A = []
>>> seq = ['oz_', A, 'ab']
>>> [value for values in seq
... for value in (values if isinstance(values, str)
... else [values])]
['o', 'z', '_', [], 'a', 'b']
For converting a list of strings into a list of character, I see two approaches:
Either use a list comprehension, containing literally each char for each of the strings:
>>> lst = ['oz_', 'A', 'ab']
>>> [char for string in lst for char in string]
['o', 'z', '_', 'A', 'a', 'b']
Or join the strings and turn the result into a list:
>>> list(''.join(lst))
['o', 'z', '_', 'A', 'a', 'b']
If A is meant to be a variable and you want to preserve it, things get more tricky. If A is a string, then that's just not possible, as A will get evaluated and is then indistinguishable from the other strings. If it is something else, then you will have to differentiate between the two types:
>>> joined = []
>>> for x in lst:
... joined += x if isinstance(x, str) else [x] # +x extends, +[x] appends
If the complete elements of the list were strings, You could use itertools.chain.from_iterable() , it takes an iterable (like list/tuple, etc) and then for each iterable element inside it, it creates a new list consisting of the elements of those inner iterables (which in this case are strings). Example -
In [5]: lst = ['oz_', 'A', 'ab']
In [6]: list(chain.from_iterable(lst))
Out[6]: ['o', 'z', '_', 'A', 'a', 'b']
As given in the updated question -
A is a list, so I don't want anything to change it.
You can do this (similar to what #SuperBiasedMan is suggesting) (For Python 3.x) -
In [14]: lst = ['oz_', 'A', 'ab',[1,2,3]]
In [15]: ret = []
In [18]: for i in lst:
....: if isinstance(i, str):
....: ret.extend(i)
....: else:
....: ret.append(i)
....:
In [19]: ret
Out[19]: ['o', 'z', '_', 'A', 'a', 'b', [1, 2, 3]]
You can use basestring in Python 2.x to account for both unicode as well as normal strings.
Please also note, the above method does not check whether a particular object in the list came from variable or not, it just breaks strings up into characters and for all other types it keeps it as it is.
>>> [a for a in ''.join(['oz_', 'A', 'ab'])]
['o', 'z', '_', 'A', 'a', 'b']
You can use chain.from_iterable either way, you just need to wrap your non strings in a list:
from itertools import chain
out = list(chain.from_iterable([sub] if not isinstance(sub, str) else sub for sub in l))
Basically what I want is this:
>>> a = ["a","a","b","c","c","c","d","e","f"]
>>> b = ["a","b","c","d","e","f"]
>>> #Do something, something like a - b
>>> result = ["a","c","c"]
Reason I want to do this, I am joining a bunch of lists of preferences and want to find which one is common among a lot of lists. The more times they occur in list a (because more lists have that element) the more weight I put towards that
You are looking for multisets, really. Use collections.Counter(), the Python implementation of a multiset:
from collections import Counter
acount = Counter(a)
bcount = Counter(b)
result = list((acount - bcount).elements())
Demo:
>>> from collections import Counter
>>> a = ['a', 'a', 'b', 'c', 'c', 'c', 'd', 'e', 'f']
>>> b = ['a', 'b', 'c', 'd', 'e', 'f']
>>> Counter(a) - Counter(b)
Counter({'c': 2, 'a': 1})
>>> list((Counter(a) - Counter(b)).elements())
['a', 'c', 'c']
You may want to retain the Counter() instances however; but if you need it the Counter.elements() method generates a sequence of elements times their count to produce your desired output again.
All you need to do is iterate over each element of b and remove it from a.
Without using multisets, you could iterate over the elements of b and remove from a. Remove only removes a single instance of the element not all instances of equivalent elements. You could do this in succinctly using map.
result = a[:] #copy a
map(result.remove, b) #remove elements of b from a
I want to make a loop for items in list that are not present in other_list, in one line. Something like this:
>>> list = ['a', 'b', 'c', 'd']
>>> other_list = ['a', 'd']
>>> for item in list not in other_list:
... print item
...
b
c
How is this possible?
for item in (i for i in my_list if i not in other_list):
print item
Its a bit more verbose, but its just as efficient, as it only renders each next element on the following loop.
Using set (which might do more than what you actually want to do) :
for item in set(list)-set(other_list):
print item
A third option: for i in filter(lambda x: x not in b, a): print(i)
list comprehension is your friend
>>> list = ['a', 'b', 'c', 'd']
>>> other_list = ['a', 'd']
>>> [x for x in list if x not in other_list]
['b', 'c']
also dont name things "list"