If I have a list like this
l=[[(1, 2), (1, 3)], [(1, 2), (2, 3)], [(1, 3), (2, 3)]]
and a dictionary like this
d={(1, 2): 3.61, (1, 3): 5.0, (2, 3): 6.0}
and I want to produce a list of lists that contains the values in d associated with the keys that appear in l, like this
newlist=[[3.61,5.0],[3.61,6.0],[5.0,6.0]]
How could I do it?
My attempt is
newlist=[v for k, v in d.items() if l[[i]]==k for i in l]
but this returns
TypeError: list indices must be integers, not list
Your attempt does not work because first of all the result should be a list of lists: your list comprehension will (if it would work) construct a list of values.
Furthermore it would loop only once over the dictionary (since that is the left for loop).
You can do this less error-prone and more efficient with:
newlist = [[d[x] for x in li] for li in l]
This will work given all tuples are keys in the dictionary d. If that is not the case, you can use .get(..) and specify a default value (for instance None):
newlist = [[d.get(x,None) for x in li] for li in l]
Related
For example:
Mainlist = [[1,2,3,4],[1,2,3,4]]
output = [1,1],[2,2],[3,3],[4,4]
Mainlist should be split based on indexes of each value in the sublists. So each value at index[0] should be grouped with the other values an index[0], and so on.
Is there a simple pythonic way to do this?
Use zip() to pair up elements of two lists.
>>> Mainlist = [[1,2,3,4],[1,2,3,4]]
>>> output = list(zip(*Mainlist))
>>> output
[(1, 1), (2, 2), (3, 3), (4, 4)]
I have a list of tuples such as:
list1=[(1,1),(2,1),(3,1),(4,0),(5,0)]
I have found the maximum element using:
max_value = max(list1, key=itemgetter(1))
This outputs: (1, 1)
I want some thing like: [(1,1),(2,1),(3,1)]
From Docs of max():
If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc, reverse=True)[0] and heapq.nlargest(1, iterable, key=keyfunc).
You could select all those values that match the max_value using list comprehension
max_value = max(list1, key=itemgetter(1))[1]
ans = [y for y in list1 if y[1] == max_value]
[(1, 1), (2, 1), (3, 1)]
I am very new at Python. I am trying to do a tuple that contains tuples in the form (1,(1,(1,'a'))). I am not allowed to use any functions. I have written a list and for each element of the list I want to take tuple.
I want to take something like (2,(3,(4,'name'))) and the result I take is (2,3,4,'name').
b = [6,5,4,3,2,1]
for i in range(len(b)):
mytuple = (i,'name')
print(i)
mytuple = (b[2],)+mytuple
print(mytuple)
Iterate reverse on b:
t="name"
for i in range(-1,-len(b)-1,-1):
t=(b[i],t)
Out: (6, (5, (4, (3, (2, (1, 'name'))))))
Considering the code snippet below -
list1 = [1,2,3,4]
list2 = [1,2,3,4]
list3 = ['a','b','c','d']
dct = dict(zip(zip(list1,list2),list3))
print(dct)
gives me,
{(1, 1): 'a', (2, 2): 'b', (3, 3): 'c', (4, 4): 'd'}
Now,
print(dct.keys())
gives me,
dict_keys([(1, 1), (2, 2), (3, 3), (4, 4)])
How can i access first element of the above list of keys?
Something like -
dct.keys[0, 0] = 1
dct.keys[0, 1] = 1
dct.keys[1, 0] = 2
dct.keys[1, 2] = 2
and so on...
Remember that a dict is unordered, and that dict.keys() may change order.
That said, to access the first element of a list, as you said, you can use list[element_index]. If the elemnt is an iterable, do that again!
So it would be
dct_keys = list(yourdict.keys())
dct_keys[0][0] = 1
dct_keys[0][1] = 1
dct_keys[1][0] = 2
dct_keys[1][1] = 2
You need to first convert the dct.keys() output to a list, and then the problem reduces to simple list-of-tuples indexing. To convert your .keys() output to a list, there are multiple available ways (check this out). Personally, I find using list comprehension as one of the simplest and most generic ways:
>>> [key for key in dct.keys()]
[(1, 1), (2, 2), (3, 3), (4, 4)]
And now simply index this list of tuples as:
>>> [key for key in dct.keys()][0][0]
1
Hope that helps.
I've got a list
a = [(1,2),(1,4),(2,6),(1,8),(3,6),(1,10),(1,6)]
If I say that:
for x in a:
if x[0]==1:
print x
I get the expected result : (1,2) (1,4) (1,8) (1,10) (1,6)
However I want to remove all the occurrences of all the tuples in the format (1,x),So
for x in a:
if x[0]==1:
a.remove(x)
I thought that all the occurences should be removed.However when i say
Print a
I get [(1,4),(2,6),(3,6),(1,6)]
Not all the tuples were removed. How do I do it.??
Thanks
I'd use list comprehension:
def removeTuplesWithOne(lst):
return [x for x in lst if x[0] != 1]
a = removeTuplesWithOne([(1,2),(1,4),(2,6),(1,8),(3,6),(1,10),(1,6)])
For me it's more pythonic than built-in filter function.
P.S. This function does not change your original list, it creates new one. If your original list is huge, i'd probably use generator expression like so:
def removeTuplesWithOne(lst):
return (x for x in lst if x[0] != 1)
This isn't the same approach as yours but should work
a = filter(lambda x: x[0] != 1, a)
You can use list comprehension like this, to filter out the items which have 1 as the first element.
>>> original = [(1, 2), (1, 4), (2, 6), (1, 8), (3, 6), (1, 10), (1, 6)]
>>> [item for item in original if item[0] != 1]
[(2, 6), (3, 6)]
This creates a new list, rather than modifying the existing one. 99% of the time, this will be fine, but if you need to modify the original list, you can do that by assigning back:
original[:] = [item for item in original if item[0] != 1]
Here we use slice assignment, which works by replacing every item from the start to the end of the original list (the [:]) with the items from the list comprehension. If you just used normal assignment, you would just change what the name original pointed to, not actually modify the list itself.
You can do it with a generator expression if you're dealing with huge amounts of data:
a = [(1,2),(1,4),(2,6),(1,8),(3,6),(1,10),(1,6)]
# create a generator
a = ((x,y) for x, y in a if x == 1)
# simply convert it to a list if you need to...
>>> print list(a)
[(1, 2), (1, 4), (1, 8), (1, 10), (1, 6)]