If there is an overlapping list such as lst in the code below, take out the column you want and make it a list Is there a built-in function?
Is there a embedded function that runs the second for loop part as one function and stores it in col_2 below?
lst = []
for i in range(5):
dicTest = { 0: 0, 1: 0, 2: 0}
dicTest[i%3] += 1
lst.append([[i]*3,dicTest,i*2,i*3])
print(lst[i])
col_2 = []
for i in range(5):
col_2.append(lst[i][2])
print(col_2)
Execution result
[[0, 0, 0], {0: 1, 1: 0, 2: 0}, 0, 0]
[[1, 1, 1], {0: 0, 1: 1, 2: 0}, 2, 3]
[[2, 2, 2], {0: 0, 1: 0, 2: 1}, 4, 6]
[[3, 3, 3], {0: 1, 1: 0, 2: 0}, 6, 9]
[[4, 4, 4], {0: 0, 1: 1, 2: 0}, 8, 12]
[0, 2, 4, 6, 8]
There is no such built-in function in the Python default list, instead
col_2 = [i[2] for i in lst]
You can abbreviate to .
The built-in function you want is implemented in the numpy module array type. For example, if you are running the following code:
import numpy
lst = numpy.array([ [1,2,3], [4,5,6], [7,8,9] ])
print lst[:,2]
Here's the result
`` [3 6 9]
© 2024 OneMinuteCode. All rights reserved.