I have 2 lists,
my_list = ['on#3','one',"$", "lo#"]
spl = ["#","$"]
I am trying to get the items in 'my_list' which contains any of the items in spl.
I tried
out_list = [item for item in my_list if item.contains("!".join(spl))]
but this gives error.
My expected output is
out_list = ["on#3","$"]
No such method as item.contains. There's item.__contains__, but you don't need to call that directly.
You want to check if any of the items in spl is contained in the item, use the builtin any:
lst = [item for item in my_list if any(x in item for x in spl)]
# ... if any(item.__contains__(x) for x in spl)]
print(lst)
# ['on#3', '$']
result = [item for item in my_list for s in spl if s in item]
And more human readable form:
result = []
for item in my_list:
for s in spl:
if s in item:
result.append(item)
Related
How do I grab that last iteration of a specific item in a python list that matches my if statement?
for example:
my_list = ["passed1", "passed2", "passed3", "vetoed"]
my_other_list = ["passed4", "passed5", "passed6", "vetoed"]
combo_list = []
combo_list.append(my_list)
combo_list.append(my_other_list)
desired_output_list = []
How do I grab the final iteration of "passed" and ONLY the final iteration of passed?
for x in combo_list:
###Grab passed3 and passed6 and append to desired_output_list
Use the last item from a list comprehension and append it to the desired output list:
my_list = ["passed1", "passed2", "passed3", "vetoed"]
my_other_list = ["passed4", "passed5", "passed6", "vetoed"]
combo_list = []
combo_list.append(my_list)
combo_list.append(my_other_list)
desired_output_list = []
for lst in combo_list:
matches = [item for item in lst if "passed" in item]
if matches:
desired_output_list.append(matches[-1])
print(desired_output_list)
# ['passed3', 'passed6']
i have this
lst = [['100','LA'],['101','NY'],['100','NC']]
lst2 = []
i need to check if there's any 100,101,etc repeated and remove the repeated numbers. So the second list would look like this
lst2=[['100','LA'],['101','NY']]
because the 100 was already added once in that second list
A quick and dirty way to do this is using a generic uniqueness filter:
def uniqueness(iterable,key=lambda x:x):
seen = set()
for item in iterable:
fitem = key(item)
if fitem not in seen:
yield item
seen.add(fitem)
You can then use it like:
list2 = list(uniqueness(lst,key=lambda x:x[0]))
If you do not specify, the filter will assume the entire element (but this will fail here, because list is not hashable).
An answer without lambda's.
nmbrs = [n for n, city in lst]
lst2 = [x for i, x in enumerate(lst) if x[0] not in nmbrs[:i]]
I want to add a value to a list of lists.
For input of [[1,2],[2,3]]
I want output of [[2,3],[3,4]]
I can do it with loops:
list_of_lists = [[1,2],[2,3]]
output = []
for list in list_of_lists:
sub_output = []
for value in list:
sub_output.append(value+1)
output.append(sub_output)
print(output)
Can I do this with list comprehension?
If I do:
[value + 1 for list in list_of_lists for value in list]
It gives me [2,3,3,4]. Can I get it to keep the sublist format somehow?
Try...
[ [n + 1 for n in inner_list] for inner_list in list ]
Yeah, you need a nested comprehension:
[[item + 1 for item in list] for list in list_of_lists]
Another way would be to use map:
map(lambda l: map(lambda i: i + 1, l), list_of_lists)
You need to nest a comprehension into that comprehension. Unpack each sublist to make it easier.
[[a+1, b+1] for a,b in list_of_lists]
I have a mixed list that looked like this:
[('1CFV',), 'NONRBP', [0.00325071141379, 0.278046326931, 0.291350892759]]
It was created with this command:
In [12]: l1 = [0.00325071141379, 0.278046326931, 0.291350892759]
In [13]: t1 = ('1CFV',)
In [14]: s1 = "NONRBP"
In [15]: mixl = [t1,s1,l1]
In [16]: mixl
Is there a way to convert it into:
['1CFV', 'NONRBP', 0.00325071141379, 0.278046326931, 0.291350892759]
I tried this, but it flattened out even the string 'NONRBP' which is not what I want:
[item for sublist in mixl for item in sublist]
>>> final = []
>>> a = [('1CFV',), 'NONRBP', [0.00325071141379, 0.278046326931, 0.291350892759]]
>>> for i in a:
... if hasattr(i, '__iter__'):
... for j in i:
... final.append(j)
... else:
... final.append(i)
...
>>> print final
['1CFV', 'NONRBP', 0.00325071141379, 0.27804632693100001, 0.291350892759]
how about
[item for item in list if type(item) is not list] +
[item for sublist in mixl if type(item) is list for item in sublist]
Solves the specific case where a list contains lists and other iterables (strings)
so this is my list:
my_list = [['a','b','c'],['d','e','f']]
I want to have the output that times 2 times each:
final_list = [['a','a','b','b','c','c'],['d','d','e','e','f','f']]
And this is what I am doing:
final_list = []
for new_list in my_list:
for my_new_list in new_list:
for i in range(2):
final_list.append(my_new_list)
but it shows:
final_list = ['a','a','b','b','c','c','d','d','e','e','f','f']
how do I fix it? and by the way I wish to do it with for loop. Thank you
This will only work for parsing an array of arrays, which I assume should be enough:
final_list = []
for sublist in my_list:
temp_list = []
for item in sublist:
temp_list += [item] * 2
final_list.append(temp_list)
def change(L):
res = []
for i in L:
temp = []
for j in i:
temp.append(j)
temp.append(j)
res.append(list)
return res