Related
l = [np.array([[1,2],[3,4]]), np.array([5,6]), np.array([[7,8],[9,10],[11,12]])]
I'm trying to flatten this list of arrays but keeping the inner arrays:
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]
I tried itertools.chain, np.concatenate, np.flatten but none of these options give the output above
Your arrays have different numbers of dimensions, you need to ensure they all are 2D:
out = np.concatenate([x[None,:] if x.ndim == 1 else x for x in l])
output:
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12]])
or with itertools.chain with a list output:
from itertools import chain
list(chain.from_iterable([x.tolist()] if x.ndim == 1 else x.tolist()
for x in l))
output: [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]
You haven't provided your desired output. But, as I understood, you are looking for this:
x = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]
print([n for m in x for n in m])
with output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Please let me know if this is not your desired output.
You basically need to flatten each subarray, concatenate them and finally reshape it.
np.concatenate(list(map(np.ravel, l))).reshape(-1, 2)
output:
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12]])
test_list = [[4, 5, 6, 8],
[2, 7, 10, 9],
[12, 16, 18, 20]]
If I want to remove column 2, that is [5,7,16] from the list, I know I can use:
[j.pop(1) for j in test_list]
however, if I want to move 2 columns at the same time, that is [5,7,16] and [8,9,20], how can I change the code, so the result is:
The modified mesh after column deletion : [[4, 6], [2, 10], [12, 18]]
Here's one other way:
columns_to_remove = (1,3)
new_object = [[x for i,x in enumerate(l) if i not in columns_to_remove] for l in test_list]
Note that this creates a new object without modifying the original test_list.
test_list = [[4, 5, 6, 8], [2, 7, 10, 9],[12, 16, 18, 20]]
removeIndex = [1,3] # The indices you want to remove
for l in test_list:
for i,v in enumerate(removeIndex):
l.pop(v-i)
print(test_list)
You can try list comprehension with required indices
NOTE: I am not altering the original list, it is creating brand new list
test_list = [[4, 5, 6, 8],
[2, 7, 10, 9],
[12, 16, 18, 20]]
[[i[0], i[2]] for i in test_list]
[[4, 6], [2, 10], [12, 18]]
You can delete multiple columns using a numpy array. Please look at numpy.delete() for documentation.
import numpy as np
test_list = [[4, 5, 6, 8],
[2, 7, 10, 9],
[12, 16, 18, 20]]
a = np.array(test_list)
a = np.delete(a, [1, 3], axis=1)
print (a)
The output will be:
[[ 4 6]
[ 2 10]
[12 18]]
You can also use numpy.delete with slice() if you want to remove a column or a set of columns.
If you want to remove 2nd and 3rd column, you can give:
np.delete(a, slice(1, 3), axis=1)
array([[ 4, 8],
[ 2, 9],
[12, 20]])
If you want to delete 2 and 4th column, you can use slice(start, stop, skip) option as follows:
np.delete(a, slice(1, None,2), 1)
Output of this will be:
array([[ 4, 6],
[ 2, 10],
[12, 18]])
If you want the numpy array to be stored back as a regular list of list, you can always do a.tolist()
You can do it easily with numpy:
import numpy as np
test_list = [[4, 5, 6, 8],
[2, 7, 10, 9],
[12, 16, 18, 20]]
a = np.array(test_list)
a = np.delete(a, [1,3], axis=1)
print(a)
#output:
[[ 4 6]
[ 2 10]
[12 18]]
I am trying to get indexes of a list store into a new list.
for example,
A = ['A', 'B', 'C',....,'Z']
and B list will select random no of indexes of A list like.
B = [[2,None,None], [1,None,None], [3,None,None],.....,[0, None,None]]
where list limit is, suppose, 10 and None will be replaced with the random no between 0 to 20 and finally the list of resultant look like,
result = [[2, 2, 3], [0, 4, 5], [8, 2, 4], [3, 8, 9]]
the first element in a sublist refers to the list A and the 2nd and third elements refer to the random selection of numbers between 0 to 10
Using random.sample.
import random
result = [random.sample(range(len(A)), 1) + random.sample(range(10), 2) for _ in range(10) ]
If you don't mind possible replication of values in the elements you can use a list comprehension, using random.randrange to generate the numbers:
result = [[random.randrange(26), random.randrange(10), random.randrange(10)] for _ in range(10)]
print(result)
Sample output:
[[18, 8, 1], [24, 1, 4], [24, 6, 5], [1, 4, 4], [7, 0, 9], [10, 7, 7], [0, 6, 9], [0, 9, 4], [6, 4, 4], [4, 2, 7]]
If you want to ensure no replication in each of elements of the list, you can use zip and random.sample to put together 3 lists of unique values and select values from those:
result = [[a, b, c] for a, b, c in zip(random.sample(range(26), 10), random.sample(range(10), 10), random.sample(range(10), 10))]
print(result)
Sample output:
[[2, 0, 1], [21, 4, 0], [11, 1, 4], [10, 7, 5], [15, 3, 3], [23, 6, 8], [25, 5, 2], [1, 9, 7], [24, 8, 9], [6, 2, 6]]
Think this basis for you
A = ['A', 'B', 'C','Z']
B = [[2,None,None], [1,None,None], [3,None,None],[0, None,None]]
for newb in B:
if newb[1] is None:
newb[1] = random.randrange(0,10)
if newb[2] is None:
newb[2] = random.randrange(0,10)
print(B)
it do like
[[2, 2, 2], [1, 6, 9], [3, 5, 7], [0, 6, 2]]
I want to return longest list within list in the sense that it is the longest list in length and if their are more then 1 list of same size then the comparison will be made according to the elements within that list.
l = [[2, 3, 5, 8], [2, 13, 15], [3, 5, 8, 13], [5, 3, 8], [5, 8, 13], [8, 5, 13]]
print(min(l, key=lambda k: (-len(k), k)))
Prints:
[2, 3, 5, 8]
I am reading Data from CSV file which comes similar to the below matrix/array
b = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
I would like to change the index of every element greater than 1 to a new row in the arraylist
this will make the above array as below
b = [[1,2],[5,6],[9,10],[3,4],[7,8][11,12]]
what i have done in python (but couldn't get the answer)
b = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
c = b
rows = len(b)
columns = len(b[0])
c[4].append(1)
count = 3
for i in range(rows):
for j in range(columns):
if i > 1:
for k in columns
list1 =
c.insert(count,list1)
count = count + 1
You can use numpy. Perform indexing and concatenate at the end:
import numpy as np
b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(np.concatenate((b[:,:2], b[:,2:])))
# [[ 1 2]
# [ 5 6]
# [ 9 10]
# [ 3 4]
# [ 7 8]
# [11 12]]
data =[]
data.append(['a',1])
data.append(['b',2])
data.append(['c',3])
data.append(['d',4])
print(data)
output
[['a', 1], ['b', 2], ['c', 3], ['d', 4]]
One line solution np.array(b).reshape(-1,2):
import numpy as np
b = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
np.array(b).reshape(-1,2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12]])
Why not just split it into two lists, and then recombine them?
new_elements = []
for i in range(len(b)):
if len(b[i]) > 2:
new_elements.append(b[i][2:])
b[i] = b[i][:2]
b.extend(new_elements)
You might want to use numpy arrays and the concatenate function.
import numpy as np
b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) # or b = np.array(b)
c = np.concatenate((b[:, :2], b[:, 2:]),0)
If you prefer working with python arrays, you can use list interpretation:
c = [row[:2] for row in b]
c.extend([row[2:] for row in b])
which returns
[[1, 2], [5, 6], [9, 10], [3, 4], [7, 8], [11, 12]]
Using a list comprehension.
Ex:
b = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
n = 2
b = [j[i:i+n] for j in b for i in range(0, len(j), n)]
b = b[0::2] + b[1::2]
print(b)
Output:
[[1, 2], [5, 6], [9, 10], [3, 4], [7, 8], [11, 12]]
Another approach would be this:
b = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
step = 2
length = len(b[0])
b = [elem[i:i+step] for i in range(0,length,step) for elem in b]
print(b)
Output:
[[1, 2], [5, 6], [9, 10], [3, 4], [7, 8], [11, 12]]