0

I have a numpy array like this:

[[[287  26]]

 [[286  27]]

 [[285  27]]

 ...

 [[290  27]]

 [[289  26]]

 [[288  26]]]

and I would like to insert an integer and make it an array like

[
[287  26 integer]
[286  27 integer]
.......]

however, since the first array has different size than what I want at the end, simply using insert() function did not work for me.

Is there a work around?

Thanks in advance.

EDIT: So the closest I came so far is the following:

outline_poses = []  # final array
 for cnt in cnts: # loop over each element
        outline_poses.append(cnt[0]) 
        outline_poses.append(SENSOR_MEASURED_HEIGHT) #Append this integer

Output:

[array([287,  26], dtype=int32), 60, array([286,  27], dtype=int32), 60,....]

How can I organize this array and make it look like [287, 26, 60],...?

2
  • 1
    Use append, not insert. Commented Dec 3, 2018 at 10:31
  • Make a target array of the right size, and copy values to it. Commented Dec 3, 2018 at 11:28

3 Answers 3

1

If I understand your problem right, you could use a list comprehension.

newList = np.array([np.append(x[0],integer) for x in myList]) 
Sign up to request clarification or add additional context in comments.

7 Comments

what is x here? I get myList but x is unclear.
x is a local iterating variable. It iterates over each element in your list myList. Since each Element is a list of one list itself the comprehension takes the first element of the sublist and adds the list [integer]. For clearer understanding of the code you can try print [x for x in myList]
Hmm I get the output of array([169, 335]), array([168, 334]), array([167, 334]), array([166, 334]), array([165, 334]), array([164, 334])], I don't think this is what I wanted.
So you can't save your data as a normal list of lists? I assume you use numpy array. That's where the error comes from I guess.
okay i'm no numpy geek but one last try: newList = np.array([np.append(x[0],integer) for x in myList])
|
0

This is a three-dimensional list you have right here...

>>> myList = [[[287,  26]],
 [[286,  27]],
 [[285,  27]],
 [[290,  27]],
 [[289,  26]],
 [[288,  26]]]

...so you'll need to access your list with two-levels of depth before inserting or appending elements into the deepest lists.

>>> myList[0][0].append(42)
>>> myList[5][0].append(42)
>>> myList
[[[287, 26, 42]],
 [[286, 27]], 
 [[285, 27]], 
 [[290, 27]], 
 [[289, 26]], 
 [[288, 26, 42]]]

What happens when you insert or append elements with shallower depths? 🤔

Appending at Depth 0

>>> myList.append('Catapult')
>>> myList
[[[287, 26, 42], 'Trebuchet'],
 [[286, 27]], 
 [[285, 27]], 
 [[290, 27]], 
 [[289, 26]], 
 [[288, 26, 42]],
 'Catapult']

Appending at Depth 1

>>> myList[0].append('Trebuchet')
>>> myList[3].append('Treebuchet')
>>> myList
[[[287, 26, 42], 'Trebuchet'],
 [[286, 27]], 
 [[285, 27]], 
 [[290, 27], 'Treebuchet'], 
 [[289, 26]], 
 [[288, 26, 42]]]

Comments

0

If I'm correct you are trying to insert an integer to all inner lists. You can use numpy concatenate method to achieve this.

integer_to_insert = 6
original_array = np.array([[[290,  27]],[[289,  26]],[[288,  26]]])
concat_integer = np.array([integer_to_insert]* original_array.shape[0]).reshape(original_array.shape[0], 1,1) 
# This is correct if you are inserting the same integer to all lists. But as long as length of this array is equal to length of original list this array can be replaced.

concatenated = np.concatenate([original_array, concat_integer], 2)
print(concatenated)
# array([[[290,  27,   6]],
# [[289,  26,   6]],
# [[288,  26,   6]]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.