This question already has answers here:
How to sort a list of lists by a specific index of the inner list?
(12 answers)
Understanding slicing
(38 answers)
Closed 3 years ago.
Input : [[0, 2], [1, 4], [2, 6]]
Description : I need to print two lists with greater value by comparing the element in the 2nd place.
Expected Output: [[1, 4], [2, 6]]
You can use sorted and specify in the key argument that you want to sort each sublist by the second element using operator.itemgetter. Then slice the returned list to select the two last sublists:
l = [[0, 2], [1, 4], [2, 6]]
from operator import itemgetter
sorted(l, key=itemgetter(1))[-2:]
Output
[[1, 4], [2, 6]]
Related
This question already has answers here:
Get unique values in List of Lists
(6 answers)
Find unique rows in numpy.array
(20 answers)
Count Distinct Values in a List of Lists
(5 answers)
Closed 6 months ago.
I have a list A containing an array with indices. I want to print all the unique index numbers by scanning each [i,j]. In [0,3], i=0,j=3. I present the expected output.
import numpy as np
A=[np.array([[[0, 1],
[0, 3],
[1, 3],
[3, 4],
[3, 6],
[4, 5],
[4, 7],
[5, 7],
[6, 4]]])]
The expected output is
A=[0,1,3,4,5,6,7]
numpy has this very cool function np.unique.
Simply to get the output A=[0,1,3,4,5,6,7]
B = np.unique(A[0])
Output :
[0 1 3 4 5 6 7]
A=[[0, 1],[0, 3],[1, 3],[3, 4],[3, 6],[4, 5],[4, 7],[5, 7],[6, 4]]
K = []
for _ in range(len(A)):
K.extend(A[_])
print(set(K))
OUTPUT:
{0, 1, 3, 4, 5, 6, 7}
A is basically a list and within list you make an array.
Do this:
def unique(A):
x = np.array(A)
print(np.unique(x))
unique(A)
if A is an array we can simply do this:
np.unique(A)
This question already has answers here:
Python find list lengths in a sublist
(7 answers)
Closed 10 months ago.
This is the list that I have:
a = [[1, 2, 3], [3, 0, 6, 4], [4, 2, 3, 6, 8]]
I would like to count the number of elements of each sublist in the list and return a new list to have the desired output of
a = [3, 4, 5]
I know how to get the total number of elements in the list with lists but not sure how to get the number of elements in each sublist...
You can try this code: a = [len(ele) for ele in a]
This question already has answers here:
List of list, converting all strings to int, Python 3
(4 answers)
Closed 11 months ago.
I know how to transform a single list of strings into integers...
But how do I transform lists of lists (strings) from into integers
I want this list:
['[1,', '3]', '[3,', '4]', '[5,', '8]', '[6,', '10]']
become a list of integers:
[1, 3], [3, 4], [5, 8], [6, 10]
The main problem with your example data, is that there's no commas between lists, you could do something like this to parse the different lists:
>>> parsed = re.findall(r'\[.+?\]', ''.join(data))
['[1,3]', '[3,4]', '[5,8]', '[6,10]']
>>> ast.literal_eval(', '.join(parsed))
([1, 3], [3, 4], [5, 8], [6, 10])
This question already has answers here:
Matrix Transpose in Python [duplicate]
(19 answers)
Closed 1 year ago.
new to python so i am writing this code that takes lists and returns a transpose version of it for example
mat = [[1,2],[3,4],[5,6]]
mat_T = mat_transpose(mat)
print(mat)
# [[1, 2], [3, 4], [5, 6]]
print(mat_T)
# [[1, 3, 5], [2, 4, 6]]
this is an example of a correct output
now how do i access the ints in a way that i can add them to a new list like the ints 1 3 5 are all in different lists but i need the in the same list and so are the ints 2 4 6 and so on if there were more ints in more lists
You don't need to access the individual values, use zip to iterate over the transposed values and map+list to convert each transposed subarray into list:
mat = [[1,2],[3,4],[5,6]]
mat_T = list(map(list, zip(*mat)))
mat_T
output:
>>> mat_T
[[1, 3, 5], [2, 4, 6]]
This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 6 years ago.
Python itertools.product() takes coma separated 1D lists and returns a product. I have a list of divisors of a number in a form
l=[[1, a1**1,a1**2,..a1**b1],[1,a2**1,..a2**b2],..[1, an**1, an**2,..an**bn]]
When I pass it to itertools.product() as an argument I don't get the desired result. How can I feed this list of lists of integers to product()?
import itertools
print([list(x) for x in itertools.product([1,2,4],[1,3])])
# [[1, 1], [1, 3], [2, 1], [2, 3], [4, 1], [4, 3]] #desired
l1=[1,2,4],[1,3] #doesn't work
print([list(x) for x in itertools.product(l1)])
#[[[1, 2, 4]], [[1, 3]]]
l2=[[1,2,4],[1,3]] #doesn't work
print([list(x) for x in itertools.product(l2)])
#[[[1, 2, 4]], [[1, 3]]]
You need to use *l2 within the product() as * unwraps the list. In this case, the value of *[[1,2,4],[1,3]] will be [1,2,4],[1,3]. Here's your code:
l2 = [[1,2,4],[1,3]]
print([list(x) for x in itertools.product(*l2)])
# Output: [[1, 1], [1, 3], [2, 1], [2, 3], [4, 1], [4, 3]]
Please check: What does asterisk mean in python. Also read regarding *args and **kwargs, you might find it useful. Check: *args and **kwargs in python explained