This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 11 months ago.
Assume I have a 3D list as following:
[[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 22, 23], [24, 25, 26]]]
I want to convert it to:
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 22, 23], [24, 25, 26]]
I do not want to get lost inside of the for loops, is there a way this can be easily implemented?
Thanks in advance.
Try summing the list with []:
sum(your_3d_list, [])
Attempt it Online!
You can use numpy module and reshape function:
import numpy as np
myList = [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 22, 23], [24, 25, 26]]]
array = np.array(myList)
array.reshape(9,3)
Output
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
[19, 20, 21],
[22, 22, 23],
[24, 25, 26]])
You can use assert in order to make sure that this is the expected array:
expected = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 22, 23], [24, 25, 26]]
array = np.array(myList)
assert array.reshape(9,3).tolist() == expected
which works fine!
Note that, array.reshape(9,3) returns a numpy array and not a list. If you want to have the expected array as a list, you can use array.reshape(9,3).tolist()
array.reshape(9,3).tolist()
Output
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 22, 23], [24, 25, 26]]
Possible solution is the following:
lst = [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 22, 23], [24, 25, 26]]]
result = [item for subitem in lst for item in subitem]
print(result)
Prints
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 22, 23], [24, 25, 26]]
You can use
import itertools
library, which is a standard python library.
use it like this:
import itertools
array = itertools.chain.from_iterable(array)
array = list(array)
the output will be:
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 22, 23], [24, 25, 26]]
You can loop through each 2d list in the 3d list, then loop through each 1d list in the 2d list
_3d_list = [[[1, 2, 3], [3, 2, 1], [1, 3, 5]], [[4, 5, 6], [6, 5, 4], [4, 6, 8]]]
final_list = []
for _2d_list in _3d_list:
for _1d_list in _2d_list:
final_list.append(_1d_list)
print(final_list)
Output:
[[1, 2, 3], [3, 2, 1], [1, 3, 5], [4, 5, 6], [6, 5, 4], [4, 6, 8]]
Related
Inputs
I have list as follow
r1 = [([[[1, 2, 3], [1, 2, 3]],
[[4, 5, 6], [4, 5, 6]]],
[[[7, 8], [7, 8]],
[[9, 10], [9, 10]]]),
([[[11, 12, 13], [11, 12, 13]],
[[14, 15, 16], [14, 15, 16]]],
[[[17, 18], [17, 18]],
[[19, 20], [19, 20]]])]
I'm going to make 2 torch tensors from the input above.
my desired output is as follow
output
output =
[tensor([[[ 1, 2, 3],
[ 1, 2, 3]],
[[ 4, 5, 6],
[ 4, 5, 6]],
[[11, 12, 13],
[11, 12, 13]],
[[14, 15, 16],
[14, 15, 16]]]),
tensor([[[ 7, 8],
[ 7, 8]],
[[ 9, 10],
[ 9, 10]],
[[17, 18],
[17, 18]],
[[19, 20],
[19, 20]]])]
My code is as follows.
output = []
for i in range(len(r1[0])):
templates = []
for j in range(len(r1)):
templates.append(torch.tensor(r1[j][i]))
template = torch.cat(templates)
output.append(template)
Is there a simpler or easier way to get the result I want?
This will do:
output = [torch.Tensor([*a, *b]) for a, b in zip(*r1)]
It concatenates the corresponding items of the two list first then create the Tensor
clip1=[1,3,4,5,6,7]
clip2=[8,10,11,12,13]
clip3=[15,16,18,19,20]
cut=[]
def toe_clip(clip1, clip2, clip3):
a= random.choice(clip1)
b= random.choice(clip2)
c= random.choice(clip3)
cut.append([a,b,c])
return
for i in range(200):
toe_clip(clip1,clip2,clip3)
cut.append(i)
print(cut)
df = pd.DataFrame(data={"Toe_clip_ID": cut})
df.to_csv("toe_clip2.csv", sep=',',index=False)
I'm trying to create a list of randomly generated number combinations. The combinations have to have 3 numbers. The first number needs to be from in the clip1 list. The second number from the clip2 list, etc. Also the combinations generated have to be unique. No combinations can be used multiple times.
I'm trying to save the generated cut list to a csv file. With the code shown, the output is separated by the numbers in range(200) with the combination and number of range alternating every other row. Does anyone know how to just get the generated cut combination list to print, without the corresponding 0-199 numbers? I've copied below the beginning of the output you get when you print the current cutlist.
[[6, 11, 18], 0, [4, 13, 19], 1, [3, 12, 15], 2, [3, 12, 20], 3, [3, 8, 18], 4, [3, 10, 16], 5, [6, 8, 18], 6, [3, 11, 20], 7, [5, 10, 18], 8, [4, 8, 18], 9, [7, 8, 19], 10, [1, 12, 19], 11, [7, 12, 18], 12, [7, 10, 18], 13, [5, 10, 18], 14, [7, 11, 15], 15, [4, 12, 16],
You can use itertools.product:
clip1=[1,3,4,5,6,7]
clip2=[8,10,11,12,13]
clip3=[15,16,18,19,20]
from itertools import product
cut = [list(i) for i in product(clip1, clip2, clip3)]
print(cut)
Output:
[[1, 8, 15], [1, 8, 16], [1, 8, 18], [1, 8, 19], [1, 8, 20], [1, 10, 15], [1, 10, 16], [1, 10, 18], [1, 10, 19], [1, 10, 20], [1, 11, 15], [1, 11, 16], [1, 11, 18], [1, 11, 19], [1, 11, 20], [1, 12, 15], [1, 12, 16], [1, 12, 18], [1, 12, 19], [1, 12, 20], [1, 13, 15], [1, 13, 16], [1, 13, 18], [1, 13, 19], [1, 13, 20], [3, 8, 15], [3, 8, 16], [3, 8, 18], [3, 8, 19], [3, 8, 20], [3, 10, 15], [3, 10, 16], [3, 10, 18], [3, 10, 19], [3, 10, 20], [3, 11, 15], [3, 11, 16], [3, 11, 18], [3, 11, 19], [3, 11, 20], [3, 12, 15], [3, 12, 16], [3, 12, 18], [3, 12, 19], [3, 12, 20], [3, 13, 15], [3, 13, 16], [3, 13, 18], [3, 13, 19], [3, 13, 20], [4, 8, 15], [4, 8, 16], [4, 8, 18], [4, 8, 19], [4, 8, 20], [4, 10, 15], [4, 10, 16], [4, 10, 18], [4, 10, 19], [4, 10, 20], [4, 11, 15], [4, 11, 16], [4, 11, 18], [4, 11, 19], [4, 11, 20], [4, 12, 15], [4, 12, 16], [4, 12, 18], [4, 12, 19], [4, 12, 20], [4, 13, 15], [4, 13, 16], [4, 13, 18], [4, 13, 19], [4, 13, 20], [5, 8, 15], [5, 8, 16], [5, 8, 18], [5, 8, 19], [5, 8, 20], [5, 10, 15], [5, 10, 16], [5, 10, 18], [5, 10, 19], [5, 10, 20], [5, 11, 15], [5, 11, 16], [5, 11, 18], [5, 11, 19], [5, 11, 20], [5, 12, 15], [5, 12, 16], [5, 12, 18], [5, 12, 19], [5, 12, 20], [5, 13, 15], [5, 13, 16], [5, 13, 18], [5, 13, 19], [5, 13, 20], [6, 8, 15], [6, 8, 16], [6, 8, 18], [6, 8, 19], [6, 8, 20], [6, 10, 15], [6, 10, 16], [6, 10, 18], [6, 10, 19], [6, 10, 20], [6, 11, 15], [6, 11, 16], [6, 11, 18], [6, 11, 19], [6, 11, 20], [6, 12, 15], [6, 12, 16], [6, 12, 18], [6, 12, 19], [6, 12, 20], [6, 13, 15], [6, 13, 16], [6, 13, 18], [6, 13, 19], [6, 13, 20], [7, 8, 15], [7, 8, 16], [7, 8, 18], [7, 8, 19], [7, 8, 20], [7, 10, 15], [7, 10, 16], [7, 10, 18], [7, 10, 19], [7, 10, 20], [7, 11, 15], [7, 11, 16], [7, 11, 18], [7, 11, 19], [7, 11, 20], [7, 12, 15], [7, 12, 16], [7, 12, 18], [7, 12, 19], [7, 12, 20], [7, 13, 15], [7, 13, 16], [7, 13, 18], [7, 13, 19], [7, 13, 20]]
Try this
clip1=[1,3,4,5,6,7]
clip2=[8,10,11,12,13]
clip3=[15,16,18,19,20]
cut=[]
for x in clip1:
for y in clip2:
for z in clip3:
cut.append([x,y,z])
print(cut)
print(len(cut))
this is my actual code:
saptamani = []
for months in range(1, 12):
luna = calendar.monthcalendar(2020, months)
for i in luna:
prepare = [m for m in i if m > 0]
if [prepare[0] != prepare[-1]]:
new_w = [prepare[0], prepare[-1]]
saptamani.append(new_w)
Now my code print this:
[[1, 5], [6, 12], [13, 19], [20, 26], [27, 31], [1, 2], [3, 9], [10, 16], [17, 23], [24, 29], [1, 1], [2, 8], [9, 15], [16, 22], [23, 29], [30, 31], [1, 5], [6, 12], [13, 19], [20, 26], [27, 30], [1, 3], [4, 10],
[11, 17], [18, 24], [25, 31], [1, 7], [8, 14], [15, 21], [22, 28], [29, 30], [1, 5], [6, 12], [13, 19], [20, 26], [27, 31], [1, 2], [3, 9], [10, 16], [17, 23], [24, 30], [31, 31], [1, 6], [7, 13], [14, 20], [21, 2
7], [28, 30], [1, 4], [5, 11], [12, 18], [19, 25], [26, 31], [1, 1], [2, 8], [9, 15], [16, 22], [23, 29], [30, 30]]
What I wanted to do is to add the month number too, to be like this
[01.01, 05.01], [05.01, 12.01]....
You think something like this perhaps?
saptamani = []
for months in range(1, 12):
luna = calendar.monthcalendar(2020, months)
for i in luna:
prepare = [m for m in i if m > 0]
if [prepare[0] != prepare[-1]]:
new_w = ["{:02d}.{:02d}".format(prepare[0],months), "{:02d}.{:02d}".format(prepare[-1],months)]
saptamani.append(new_w)
This writes saptamani variable like this:
[['01.01', '05.01'], ['06.01', '12.01'], ['13.01', '19.01'], ['20.01', '26.01'], ['27.01', '31.01'], ['01.02', '02.02'], ['03.02', '09.02'], ['10.02', '16.02'], ['17.02', '23.02'], ['24.02', '29.02'], ['01.03', '01.03'], ['02.03', '08.03'], ['09.03', '15.03'], ['16.03', '22.03'], ['23.03', '29.03'], ['30.03', '31.03'], ['01.04', '05.04'], ['06.04', '12.04'], ['13.04', '19.04'], ['20.04', '26.04'], ['27.04', '30.04'], ['01.05', '03.05'], ['04.05', '10.05'], ['11.05', '17.05'], ['18.05', '24.05'], ['25.05', '31.05'], ['01.06', '07.06'], ['08.06', '14.06'], ['15.06', '21.06'], ['22.06', '28.06'], ['29.06', '30.06'], ['01.07', '05.07'], ['06.07', '12.07'], ['13.07', '19.07'], ['20.07', '26.07'], ['27.07', '31.07'], ['01.08', '02.08'], ['03.08', '09.08'], ['10.08', '16.08'], ['17.08', '23.08'], ['24.08', '30.08'], ['31.08', '31.08'], ['01.09', '06.09'], ['07.09', '13.09'], ['14.09', '20.09'], ['21.09', '27.09'], ['28.09', '30.09'], ['01.10', '04.10'], ['05.10', '11.10'], ['12.10', '18.10'], ['19.10', '25.10'], ['26.10', '31.10'], ['01.11', '01.11'], ['02.11', '08.11'], ['09.11', '15.11'], ['16.11', '22.11'], ['23.11', '29.11'], ['30.11', '30.11']]
Your script wrote integer values but for getting something like "01.01" you need string instead.
So, I have a list that looks something like this
[[[1, 2], [3, 4], [5, 6]],
[[7, 8], [9, 10], [11, 12]],
[[13, 14], [15, 16], [17, 18]],
[[19, 20], [21, 22], [23, 24]]]
And I want it to look like this
[[3, 7, 11],
[15, 19, 23],
[27, 31, 35],
[39, 43, 27]]
that is 3 = sum([1, 2]), 7 = sum([3, 4]), ....
I have tried nesting for loops but I haven't found anything that got the desired result, does anyone know how I could do this?
This will do the job quite nicely and imo is more readable than list comprehensions.
lists = [[[1, 2], [3, 4], [5, 6]],
[[7, 8], [9, 10], [11, 12]],
[[13, 14], [15, 16], [17, 18]],
[[19, 20], [21, 22], [23, 24]]]
new_lists = []
for nested in lists:
new_ls = []
for ls in nested:
new_ls.append(sum(ls))
new_lists.append(new_ls)
>>> new_lists
[[3, 7, 11], [15, 19, 23], [27, 31, 35], [39, 43, 47]]
You could also use list comprehensions:
[[sum(x) for x in triple] for triple in lists]
In the above list comprehension, triple will be your list of three doubles so the first for loop will be covering these. x will then be each list of doubles, inside of the triple so we are summing it, while keeping it inside the original triple by using this bracket around:
[sum(x) for x in triple]
output:
[[3, 7, 11], [15, 19, 23], [27, 31, 35], [39, 43, 47]]
If you are happy to use a 3rd party library, you can use NumPy and sum along a single dimension:
import numpy as np
A = np.array([[[1, 2], [3, 4], [5, 6]],
[[7, 8], [9, 10], [11, 12]],
[[13, 14], [15, 16], [17, 18]],
[[19, 20], [21, 22], [23, 24]]])
res = A.sum(2)
Result:
array([[ 3, 7, 11],
[15, 19, 23],
[27, 31, 35],
[39, 43, 47]])
See also: What are the advantages of NumPy over regular Python lists?
I have a three dimensional array in this format:
x = [
[[1,2,3,4,5],[6,7,8,9,10]],
[[11,12,13,14,15],[16,17,18,19,20]],
[[21,22,23,24,25],[26,27,28,29,30]],
[[21,22,23,24,25]]
]
I'd like to split it into two, three dimensional arrays in this format:
y = [
[[1,2,3],[6,7,8]],
[[11,12,13],[16,17,18]],
[[21,22,23],[26,27,28]],
[[21,22,23]]
]
z = [
[[4,5],[9,10]],
[[14,15],[19,20]],
[[24,25],[29,30]],
[[24,25]]
]
I came up with this list comprehension for creating y:
[j[:3] for i in x for j in i]
Which returns this:
[[1, 2, 3], [6, 7, 8], [11, 12, 13], [16, 17, 18], [21, 22, 23], [26, 27, 28], [31, 32, 33]]
But as you'll see, it doesn't maintain the same multi-dimensional shape. Does anyone have any ideas?
You need to iterate one level deeper:
x = [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]], [[21, 22, 23, 24, 25], [26, 27, 28, 29, 30]], [[21, 22, 23, 24, 25]]]
y = [[i[:3] for i in b] for b in x]
z = [[i[-2:] for i in b] for b in x]
Output:
[[[1, 2, 3], [6, 7, 8]], [[11, 12, 13], [16, 17, 18]], [[21, 22, 23], [26, 27, 28]], [[21, 22, 23]]]
[[[4, 5], [9, 10]], [[14, 15], [19, 20]], [[24, 25], [29, 30]], [[24, 25]]]
Move your inner most loop into a nested comprehension so that the inner lists are preserved:
y = [[j[:3] for j in i] for i in x]