This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 3 years ago.
I have this code to make an array that shows the index of "frq_peak" that contains each elements of "F".
a =[]
for i in range(len(F)):
if i == 0:
a.append(np.where(frq_peak[6] == F[i]))
elif F[i] != F[i-1]:
a.append(np.where(frq_peak[6] == F[i]))
a
the problem is that "a" become a combination of multiple array but I want to have just one. what should I do?
You can use this code, where your array is [[1, 2, 3], [4, 5, 6], [7, 8, 9]]:
from functools import reduce
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
singleArray = reduce(lambda x, y: x+y, arr)
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:
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:
How do I check if there are duplicates in a flat list?
(15 answers)
Closed 6 years ago.
So I have a list called puzzle which contains the follow lists:
puzzle = [[1, 3, 5, 5, 4],
[3, 5, 1, 3, 4],
[2, 3, 4, 5, 1],
[1, 5, 3, 2, 2],
[5, 4, 1, 3, 2]]
I would like to check each list inside puzzle and test if there are any duplicate numbers that are not zero, in which case the code would return false. How can I do this?
Almost the same approach -- except that you'd run it on a sub-list without zeros in it.
def has_dup(lst):
no_zeros = [x for x in lst if x != 0]
return len(set(no_zeros)) != len(no_zeros)
is_valid = any(has_dup(x) for x in puzzle)