This question already has answers here:
Finding max value in the second column of a nested list?
(5 answers)
Closed 2 years ago.
I'm catching up with Python3 so I hope I can get some slack :P
I have a list of lists, say:
results=[[1, 5000],
[2, 5000],
[3, 6666],
[4, 6250],
[5, 6000],
[6, 5833],
[7, 5714],
[8, 6250],
[9, 6111]]
And I would like to get the entry which has the biggest second value (in this case, the pair [3, 6666]).
Could I have some quick reference on how to get it?
Thanks!
Here is one approach using itemgetter()
import operator
>>> max(results, key=operator.itemgetter(1))
[3, 6666]
>>>
Related
This question already has answers here:
Faster way to take two lists of integers and create pairs of type list
(1 answer)
How to merge lists into a list of tuples?
(10 answers)
How to join lists element-wise in Python?
(4 answers)
How to pair two list in python
(2 answers)
Closed last year.
I have a simple problem that I'm finding problematic and I need some help with it. I want to convert two lists into data points.
example:
Data:
A = [1,2,3,4,5]
B = [6,7,8,9,10]
Wanted output:
C = [[1,6], [2,7], [3,8], [4,9], [5,10]]
It's like a bijective function in mathematics, but I have no idea how to implement it in code. It doesn't have to be lists. Vectors, Matrices, Arrays. Really anything will do. I have thousands of observations and I want a quick way to group them in the above fashion so I can analyze them. Right now, I'm doing a bunch of workarounds by exporting it from different software. I've tried using For loops and even indexing, but I just can't seem to get it right. New to writing code! Please help!
P.S. I'm using Python.
A = [1,2,3,4,5]
B = [6,7,8,9,10]
C = list(map(lambda x, y:[x,y], A, B))
print (C)
This would give the following output
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
Hope this was helpful :)
import itertools
A = [1,2,3,4,5]
B = [6,7,8,9,10]
list(itertools.izip(A,B))
Result:
[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
or
with list comprehension like this :
[list(k) for k in itertools.izip(A,B)]
Result:
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
This question already has answers here:
How to remove an element from a list by index
(18 answers)
Closed last year.
I have a list that contains some stuff similar to the below entry:
[[1, 'Potato', 2, 'Bag'], [2, 'Banana, 1, 'Bunch']].
These elements represent in order the item's UPC, Name, Quantity, and unit type. What I want to do is delete all the items at a certain index to change the list from the above to [[1, 'Potato', 2, 'Bag']]. I tried a couple of methods, but I either used them wrong or they weren't supposed to be used there (I used del, pop, and remove). How do I go about doing this?
del works for your example
>>> data = [[1, 'Potato', 2, 'Bag'], [2, 'Banana', 1, 'Bunch']]
>>> del data[1]
>>> data
[[1, 'Potato', 2, 'Bag']]
This question already has answers here:
Transpose/Unzip Function (inverse of zip)?
(14 answers)
Splitting a list of sequences into two lists efficiently [duplicate]
(2 answers)
Closed 1 year ago.
I know this has already been asked in other thread but I recall a neater way of doing this:
so here is a code that i know how to unpack things:
list = [[1, "tom"], [4, "jane"], [2, "peter"], [5.5, "Ursala"]]
points=[p for p, n in list]
name=[n for p, n in list]
So I know this is how I extract lists from list of list
but i remember there is some neater way of doing this.
It's something like.
points,name = list
it gives me however the error: too many values to unpack (expected 2)
is this method for dictionary? not applicable for list of list?
Using zip you get tuples, convert those tuples to list using map
* is used for unpacking the iterable
l = [[1, "tom"], [4, "jane"], [2, "peter"], [5.5, "Ursala"]]
points,name = map(list, zip(*l))
# just to point out numpy is not only for numbers
import numpy as np
l = [[1, "tom"], [4, "jane"], [2, "peter"], [5.5, "Ursala"]]
ln = np.array(l)
points, name = ln[:, 0], ln[:, 1]
PS: list is already reserved by python use some other unreserved name
This question already has answers here:
Sum a list of lists to get a sum list Python
(2 answers)
Closed 5 years ago.
First off, I am really new to python, and programming in general, and I'm struggling to grasp the concept of nested for loops and nested lists.
In my code below I am trying to take each list inside the list list1 and sum them using a for loop. I am aware that the range function would help somehow.
Code:
def sum_list(list1):
list_of_sums = []
total = 0
for l in list1:
for value in l:
total = total + value
list_of_sums.append(total)
return list_of_sums
Input test:
list1 = [[4, 7, 9], [4, 5, 2], [4, 5, 6]]
print(sum_list(list1))
Output:
[4, 11, 20]
Desired output:
[20, 11, 15]
You have some logical issues in your code. Think carefully when you should reset total and when to append result to the list_of_sums.
def sum_list(list):
list_of_sums = []
for sublist in list:
total = 0
for value in sublist:
total += value
list_of_sums.append(total)
return list_of_sums
you can achieve this by using list comprehension, it's one of the best thing provided by python. it really shirnks you code and yet easy to understand.
following post my help. You can google more on list comprehension, if you like
http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/
def sum_list(list1):
return [sum(inner_list) for inner_list in list1]
print sum_list([[4, 7, 9], [4, 5, 2], [4, 5, 6]])
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 7 years ago.
Trying to flatten a list of lists into one list of unique values:
[[1, 2, 3], [4, 5, 6], [6, 7, 8]] into [1,2,3,4,5,6,7,8] where the first list might contain N number of lists.
You could try it like this:
a = [[1,2,3,4], [5,6,7,8]]
nxtList = []
for i in a:
for j in i:
nxtList.append(j)
This won't guarantee that your values are unique, but if you know ahead of time that the list (a) always contains unique values then it won't be a problem.