Let's make this short.
I have a list of lists, for example
a = [[1.0, 2.0], [6, 8]]
What I would like to do, is to convert all values of the first sublist into integers, so the array will be
a = [[1, 2], [6, 8]]
Can you please tell me how to do it? I struggled with map already, yet I can't seem to find the correct usage. Thanks in advance.
You can use a nested list comprehension:
[[int(y) for y in x] for x in a]
Or with map:
[list(map(int, x)) for x in a]
You can use list comprehension for the first list (that needs transformation), and add the second list (which you don't need to change).
print([[int(aa) for aa in a[0]], a[1]])
Out[46]: [[1, 2], [6, 8]]
Unlike the other solutions, this does not change the 2nd part of the list in any way.
If you only want to change the first delivery list, an option
[a for a in map(lambda n, x: list(x if n else int(x) for x in a[n]), (0, 1), a)]
You can assign it to a variable and then print to the screen.
b = [a for a in map(lambda n, x: list(x if n else int(x) for x in a[n]), (0, 1), a)]
print(b)
remembering that it will only transform the values in the left list
Related
I would like to retrieve specific elements within a list of lists without using list comprehension, loops, or any iterative approach in Python.
For example, given this list:
[[1,2,3,4],[3,4],[5,6,7]]
and this vector:
[0,0,1]
I would like to retrieve the 0th element on the 0th list, the 0th element of the 1st list, and the 1st element of the 2nd list:
[1,2,3,4][0] -> 1
[3,4][0] -> 3
[5,6,7][1] -> 6
Which should give this result:
[1,3,6]
Is this possible in python?
This is an alternate:
x = [[1,2,3,4],[3,4],[5,6,7]]
y = [0,0,1]
res = []
for i in range(len(x)):
res.append(x[i][y[i]])
Using a list comprehension with zip() is one of the most Pythonic way to achieve this:
>>> my_list = [[1,2,3,4],[3,4],[5,6,7]]
>>> my_vector = [0,0,1]
>>> [x[i] for x, i in zip(my_list, my_vector)]
[1, 3, 6]
However, since OP can not use list comprehension, here's an alternative using map() with lambda expression as:
>>> list(map(lambda x, y: x[y], my_list, my_vector))
[1, 3, 6]
In the above solution, I am explicitly type-casting the object returned by map() to list as they return the iterator. If you are fine with using iterator, there's no need to type-cast.
you can use zip
l = [[1,2,3,4],[3,4],[5,6,7]]
i = [0,0,1]
op = []
for index, element in zip(i, l):
op.append(element[index])
output
[1, 3, 6]
using map you can do this way
a = [[1,2,3,4],[3,4],[5,6,7]]
b = [0, 0, 1]
result = list(map(lambda x,y: x[y], a,b))
print(result)
output
[1, 3, 6]
I am trying to multiply a list by a nested list.
For example if we have
a=[1,2,3]
b=[[2,4,6],[3,5,7]]
I am trying to multiply each element of 'list b' by elements of 'list a'
I am trying to achieve something along these lines
product=[[2,8,18],[3,10,21]]
Here is what I tried so far
product=[[w*c for c in v] for w, v in zip(a, b)]
and result I got is
[[-4.0, -8.0, -12.0], [0.0, 0.0, 0.0]]
which is clearly not what I expected
I am not allowed to use numpy by the way.
I would have a inner zip, because a outer zip will zip with a and b, with each pairs of list with each element in a, so it will fail.
print([[y*z for y,z in zip(a,x)] for x in b])
Output:
[[2, 8, 18], [3, 10, 21]]
a=[1,2,3]
b=[[2,4,6],[3,5,7]]
for idxa, val in enumerate(a):
for idxb, sublist in enumerate(b):
b[idxb][idxa] = sublist[idxa] * val
Results in b equal to [[2, 8, 18], [3, 10, 21]] and may be easier to reason about if just starting out with Python rather than dealing with list comprehension and zip or other functional programming calls.
You can use zip in combination with list comprehension:
product = [[i*j for i,j in zip(a,k)] for k in b]
I have a Python list like:
mylist = [1,2,3,4,5,6,7,8]
And I want to run an operation on each two consecutive variables. For example I want to sum each two consecutive variables in the list and put them into another list:
newlist = [1+2, 3+4, 5+6, 7+8]
But how can I do that in Python? I didn't know where to start. Should I use two nested for loops, or enumerate, or zip function? I am confused.
My favorite way to do this is with an explicit list_iterator.
itr = iter(mylist)
newlist = [x + y for x, y in zip(itr, itr)]
zip advances the iterator by two elements each time it yields a pair. The benefit of using an iterator over slicing is that it doesn't require you to make two half-copies of the list before zipping.
If you don't like two lines, Python 3.8 can fold the assignment into an expression:
from operator import add
newlist = list(map(add, (i:=iter(mylist)), i))
(For some reason, you can't use an assignment expression in a list comprehension.
[x + y for x, y in zip((i:=iter(mylist)), i)]
is a syntax error, but
t = zip((i:=iter(mylist)), i)
[x + y for x, y in t]
is fine. I suspect it has something to do with scoping, but I don't know the technical details.)
Solution with zip():
out = [a+b for a, b in zip(mylist[::2], mylist[1::2])]
print(out)
Prints:
[3, 7, 11, 15]
The range() function defaults to increment the sequence by 1, but one can increment the value by adding a third parameter - range(start, stop, step_size).
You can try this:-
res = [mylist[i]+mylist[i+1] for i in range(0,len(mylist)-1, 2)]
print(res)
Output:-
[3, 7, 11, 15]
You can use this list comprehension:
mylist = [1,2,3,4,5,6,7,8]
mylist =[n+mylist[i-1] for i,n in enumerate(mylist) if i%2]
print(mylist)
Output:
[3, 7, 11, 15]
I am trying to use list comprehension to remove a number of items from a list by just keeping those not specified.
For example if I have 2 lists a = [1,3,5,7,10] and b = [2,4] I want to keep all items from a that are not at an index corresponding to a number in b.
Now, I tried to use y = [a[x] for x not in b] but this produces a SyntaxError.
y = [a[x] for x in b] works fine and keeps just exact the elements that i want removed.
So how do I achieve this? And on a side note, is this a good way to do it or should I use del?
You can use enumerate() and look up indexes in b:
>>> a = [1, 3, 5, 7, 10]
>>> b = [2, 4]
>>> [item for index, item in enumerate(a) if index not in b]
[1, 3, 7]
Note that to improve the lookup time, better have the b as a set instead of a list. Lookups into sets are O(1) on average while in a list - O(n) where n is the length of the list.
Guess you're looking for somthing like :
[ x for x in a if a.index(x) not in b ]
Or, using filter:
filter(lambda x : a.index(x) not in b , a)
Try this it will work
[j for i,j in enumerate(a) if i not in b ]
after this:
y = [a[x] for x in b]
just add:
for x in y:
a.remove(x)
then you end up with a stripped down list in a
I need to find the smallest value in a series of lists. I understand the code for the smallest value in just one list:
>>> x = [1, 2, 3, 4, 5]
>>> print (min(x))
1
Simple enough. However, I would like to know if there is a way to write code that finds the smallest value for each list I have without stopping and adjusting the code (whether by an iterative process or some other means). Any help would be greatly appreciated. Thanks in advance!
First, make a list of lists out of your separate lists. For example, if you have lists A, B and C, the list of lists would be [A,B,C].
Now, to get a list of all the minimum values for each list in a list of lists lst:
[min(x) for x in lst]
To get the global minimum:
min(x for sublist in lst for x in sublist)
Demo:
>>> lst
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [min(x) for x in lst]
[1, 4, 7]
>>> min(x for sublist in lst for x in sublist)
1
Edit in reaction to OP's comment:
I just want the minimum value for each list. I don't need to compile all of the minimum values together into a new list
If you just want to print the minimum values for each list A, B, C, ... , you can do:
for lst in (A,B,C): # put as many lists as you like into the parentheses
print(min(lst))
Edit in reaction to
I am only accessing one list (in this case, the values are well depths) at a time. [..] What I would like to know is how to write a code that finds the smallest value in a list given that the iterator essentially changes the values in said list.
Just print(min(lst)) each time lst has changed.
Assuming you've got a list of lists, this should do it:
minimums = [min(l) for l in lists]