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])
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:
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:
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]]
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Python Convert String List to List
(3 answers)
Closed 5 years ago.
I have a list of strings like this:
L=['[1,2,3]','[3,4,5,6]','[9,8,7]']
I want to make a list of list elements like this:
Y=[[1,2,3],[3,4,5,6],[9,8,7]]
How can I do it in python?
You can use ast.literal_eval to evaluate each string as a python literal within a list comprehension
>>> from ast import literal_eval
>>> [literal_eval(i) for i in L]
[[1, 2, 3], [3, 4, 5, 6], [9, 8, 7]]
Every element of your array is a valid JSON so you could do
import json
Y = [json.loads(l) for l in L]
You could also do this manually:
>>> L=['[1,2,3]','[3,4,5,6]','[9,8,7]']
>>> [[int(x) for x in l[1:-1].split(',')] for l in L]
[[1, 2, 3], [3, 4, 5, 6], [9, 8, 7]]
Or without split(), as suggested by #Moinuddin Quadri in the comments:
>>> [[int(x) for x in l[1:-1:2]] for l in L]
[[1, 2, 3], [3, 4, 5, 6], [9, 8, 7]]
This question already has answers here:
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 9 years ago.
A function named add_r that takes a list as argument and adds all numeric values in
all levels of the provided list. Assume that the input list will always be a list of numbers or sub-lists that may contain further sub-lists and/or numbers.
For example, add_r( [[1, 2], [3], [[4, 5, 6], [7, 8, [9, 10]]]]) should return 55. Take into
account that the input list provided as argument to the function can contain sublists at any depth.
Use a recursive function:
from collections import Iterable
def add_r(lis):
for x in lis:
if isinstance(x, Iterable):
for y in add_r(x):
yield y
else:
yield x
>>> lis = [[1, 2], [3], [[4, 5, 6], [7, 8, [9, 10]]]]
>>> sum(add_r(lis))
55
On py2.x you can also use compiler.ast.flatten:
>>> from compiler.ast import flatten
>>> sum(flatten(lis))
55