to be like that : [[1,0,0,0,0],[0.57735027, 0.57735027, 0.57735027,0,0],[0.57735027, 0.57735027, 0.57735027,0,0],and so on]
Asked
Active
Viewed 229 times
-3
-
2Please don't post code as images. Add the actual list you are starting with as text and use the `<>` button in the editor to format it as code. – Mark Mar 25 '20 at 01:52
-
1This question's title is a bit misleading,maby you should say: "How to convert list of different length of arrays into arrays of same lenth in python" – xkcdjerry Mar 25 '20 at 01:56
-
1Does this answer your question? [Convert Python sequence to NumPy array, filling missing values](https://stackoverflow.com/questions/38619143/convert-python-sequence-to-numpy-array-filling-missing-values) – Mark Mar 25 '20 at 02:00
4 Answers
1
Perhaps, you're looking for something like:
data = [[1],[0.52, 0.53, 0.54],[],[0.1, 0.2, 0.3, 0.4, 0.5]]
max_size = 0
# identify the maximum length, for any of the lists in data
for arr in data:
max_size = max(max_size, len(arr))
# extend each lists as required
for arr in data:
# NOTE: the following should work, since the (max_size >= len(arr)) condition will always hold in this case
arr.extend([0] * (max_size - len(arr)))
print(data)
NOTE: there will be better approaches as well. But, this might give you a direction.

dvlper
- 462
- 2
- 7
- 18
-1
You could try doing something like this:
yourArray = [[1,0,0,0,0],[0.57735027, 0.57735027, 0.57735027,0,0],[0.57735027, 0.57735027, 0.57735027,0,0],and so on]
outputArray = []
for subArr in yourArray:
[outputArray.append(i) for i in subArr]

Nimantha
- 6,405
- 6
- 28
- 69

EnriqueBet
- 1,482
- 2
- 15
- 23
-1
master_array = []
for a in yourArray:
master_array.extend(a)

Matt
- 478
- 4
- 14
-
1While this may answer the question, it was flagged for review. Answers with no explanation are often considered low-quality. Please provide some commentary for why this is the correct answer. – Dan Mar 25 '20 at 17:10
-1
You can use this(assuming it's called lists):
mxlen=max(len(i) for i in lists)
for i in lists:
i.extend([0]*(mxlen-len(i)))

xkcdjerry
- 965
- 4
- 15