The following is the first line from my list l:
[0.0, 753.128, 990.43, 686.832, 366.922, -93.454, 1.0]
This is the result of:
print l[0]
I want to take all the first element from all such *lines of my list and assign them to another list. How can I do it in python?
Using a list comprehension:
lists = [[1,2,3], [4,5,6]]
[ l[0] for l in lists ]
That would do it. Nicer is to use map: you map a list of lists to a list of their heads:
map( lambda l: l[0], lists )
If performance is important, you may want to create an iterator over the heads of your lists:
heads = itertools.imap( lambda l: l[0], enumerate(lists))
for head in heads:
print head
Basic list comprehension:
another_list = [sublist[0] for sublist in l]
Try this:
a = [[1,2,3], ['a','b','c'], [True, False]]
first_elements = [e[0] for e in a]
print first_elements
>>> [1, 'a', True]
Something like this?
>>> a = [1, 2, 3, 4]
>>> b = [5,6,7,8]
>>> ab = [a, b]
>>> map (lambda x : x[0], ab)
[1, 5]
newlist=[]
for l in lst:
newlist.append(l[0])
Related
I have two lists like this:
a = [[1,2,3],[2,3,4],[5,6,7],[7,8,9]]
b = [1,2]
I would now like to filter list a, to keep only the items which contain the items in list b. So the desired output would look like this:
[[1,2,3],[2,3,4]]
I have tried some nested list comprehensions, which I could think of, but could not get the desired output. Any advice is appreciated.
you could try something like this :
print([i for i in a if any(map(i.__contains__,b))])
>>> [[1, 2, 3], [2, 3, 4]]
I would try something like this:
a = [[1,2,3],[2,3,4],[5,6,7],[7,8,9]]
b = [1,2]
result = [lst for lst in a if any(x in lst for x in b)]
A combination of list comprehension and sets would yield the wanted result. Note; I assume that repeated items and ordering is not of interest, if this is the case - a set won't work since it ignores ordering and only allows unique items.
A simple list comprehension would do, like below
filter_items = set(filter_items)
[sublist for sublist in original_list if not set(sublist).isdisjoint(filter_items)]
There's mainly one interesting part of this list comprehension, namely the if not set(sublist).isdisjoint(filter_items). Here you only keep the sublist if the set of sublist is not disjoint of the set filter_items i.e. none of the filter_items is in the sublist.
for your given example the provided answer would yield the following:
>>> a = [[1,2,3],[2,3,4],[5,6,7],[7,8,9]]
>>> b = set([1,2])
>>> [sublist for sublist in a if not set(sublist).isdisjoint(b)]
[[1, 2, 3], [2, 3, 4]]
Using a set approach the in can be mimic with an intersection:
a = [[1,2,3],[2,3,4],[5,6,7],[7,8,9]]
b = [1,2]
b_as_set = set(b)
out = [l for l in a if b_as_set.intersection(l)]
# [[1, 2, 3], [2, 3, 4]]
I would try something like this.
a = [[1,2,3],[2,3,4],[5,6,7],[7,8,9]]
b = [1,2]
print([lst for lst in a if any([item in b for item in lst])])
I am trying to find the index of elements that contain similar entries in lists of lists.
Data might contained duplicated entries as well as sub-lists.
For example:
list_A = [['A',[1],'a',],['B',[2],'b'],['C',[3],'c'],['D',[4],'d'],['E',[5],'e'],['A',[1],'a',]]
list_B = [['A','a'],['E','e']]
The desired output should be:
[0, 4, 5]
If you need the number shown at index 1, you can use:
res = []
for lb in list_B:
for la in list_A:
if all([x in la for x in lb]):
res.append(la[1][0])
print sorted(res)
# OUTPUT: [1, 1, 5]
if you want the index in list_A, you should use:
res = []
for lb in list_B:
for n, la in enumerate(list_A):
if all([x in la for x in lb]):
res.append(n)
print sorted(res)
# OUTPUT: [0, 4, 5]
Try this:
import itertools
list_A = [['A',[1],'a',],['B',[2],'b'],['C',[3],'c'],['D',[4],'d'],['E',[5],'e'],['A',[1],'a',]]
list_B = [['A','a'],['E','e']]
desired_output = []
for i,j in itertools.product(enumerate(list_A),list_B):
if all(k in i[1] for k in j):
desired_output.append(i[0])
output:
[0, 4, 5]
If you eliminate the inner lists from each element in list_A, you can compare the elements directly with list_B:
list_A = [['A',[1],'a'], ['B',[2],'b'], ['C',[3],'c'], ['D',[4],'d'],
['E',[5],'e'], ['F',[6],'f']]
list_B = [['A','a'],['D','d'],['E','e']]
new_A = ([a, b] for a, _, b in list_A)
common = [i for i, x in enumerate(new_A) if x in list_B]
If you're sure that each element of list_B is uniquely present, then the following works too:
new_A = [[a, b] for a, _, b in list_A]
common = [new_A.index(x) for x in list_B]
I have the following list:
a = [[['trial1', 'trial2'], 4], [[], 2]]
and I want to remove the list that have inside an empty list.So the following result:
c = [[['trial1', 'trial2'], 4]]
I am using the following code:
c = []
for b in a:
temp =[x for x in b if x]
if len(temp)>1:
c.append(temp)
It works ok, but it seems not to be a 'good way' of doing this task. Is there a more elegant way?
c = [l for l in a if [] not in l]
Use a list comprehension:
c = [x for x in a if [] not in x]
You could use list comprehension and all function to check whether everything in the list evaluted to the True:
c = [i for i in a if all(i)]
print(c)
[[['trial1', 'trial2'], 4]]
You may use filter built-in function.
a = [[['trial1', 'trial2'], 4], [[], 2]]
assert filter(lambda o: [] not in o, a) == [[['trial1', 'trial2'], 4]]
Since in Python 3 filter is lazy, you may need explicit conversion to list.
assert list(filter(lambda o: [] not in o, a)) == [[['trial1', 'trial2'], 4]]
I have some syntax :
l_one = [ x for x in someList ] # list with duplicates
l = []
l_two = [ l.append( y ) for y in l_one if y not in l ] # duplicates removed
Both l and l_two are the same lists without duplicates. Is there way to reduce lines and maybe have oneliner?
EDIT :
Correction - l_two are the "Noned" list.
Actually, they aren't the same. .append() returns None because it modifies the list in place, so l_two is a list with a bunch of Nones. However, l will be the list with no dupes.
If you want to remove duplicates from a list, you can make it into a set:
l_two = list(set(l_one))
Note that this will remove order.
Try using a for-loop instead of a list comprehension if you want to use unhashable types:
l_one = [x for x in someList]
l_two = []
for i in l_one:
if i not in l_two:
l_two.append(i)
Or:
from itertools import groupby
l_two = [key for key, value in groupby(l_one)]
IF I'm understanding correctly, you're starting with a list called someList which may have duplicates, and you want to end up with the same list but with duplicates removed?
You could start by removing your first line of code, which just duplicates someList into a new (but identical) list called l_one:
>>> someList = [ 3,1,4,1,5,9,2,7 ]
>>> l = []
>>> [ l.append(y) for y in someList if y not in l]
[None, None, None, None, None, None, None]
>>> print l
[3, 1, 4, 5, 9, 2, 7]
>>>
This works even if the elements of someList are themselves lists:
>>> l = []
>>> someList = [[1,2],[2,1],[1,2],[3,4]]
>>> l = []
>>> [ l.append(y) for y in someList if y not in l]
[None, None, None]
>>> print l
[[1, 2], [2, 1], [3, 4]]
>>>
Take a 2d list.
I want to make a new list with only the ith element from each list.
What is the best way to do this?
I have:
map(lambda x: x[i], l)
Here is an example
>>> i = 0
>>> l = [[1,10],[2,20],[3,30]]
>>> map(lambda x: x[i], l)
[1, 2, 3]
Use list comprehension:
i = 1
data = [[1,10],[2,20],[3,30]]
result = [d[i] for d in data] # [10, 20, 30]
Also see this question on list comprehension vs. map.