I have a nested array in a nested array that is within ANOTHER nested array, i'm trying to move the baby nested array one position behind the second nested array.
So, for example ['ZOMB',15]
one position behind replacing the None, and the None will replace the zomb, and I'm trying to do this for all rows applicable
field = [ [['PEASH', 5], None, None, None, None, None, ['ZOMB', 15]],
[['PEASH', 5], None, None, None, None, None, ['ZOMB', 15]],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None] ]
CodePudding user response:
You can try inserting ['ZOMB', 15] into index you want to swap with below;
field[0].insert(5, field[0].pop(6))
Output;
[[['PEASH', 5], None, None, None, None, ['ZOMB', 15], None], [['PEASH', 5], None, None, None, None, None, ['ZOMB', 15]], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None]]
So basically you can tailor below method for your need
My_List.insert(index_to_insert,My_List.pop(index_to_remove))
CodePudding user response:
Try this:
baby = ['ZOMB', 15]
for arr in field:
for i in range(1, len(arr)):
if arr[i] == baby:
arr[i - 1], arr[i] = arr[i], arr[i - 1]
Output:
[[['PEASH', 5], None, None, None, None, ['ZOMB', 15], None],
[['PEASH', 5], None, None, None, None, ['ZOMB', 15], None],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None]]
CodePudding user response:
I understood the question different than the other peers and will drop my solution here if needed:
field = [ [['PEASH', 5], None, None, None, None, None, ['ZOMB', 15]],
[['PEASH', 5], None, None, None, None, None, ['ZOMB', 15]],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None] ]
def a(x):
return 0 if type(x) == list else 1
new_field = []
for i in field:
new_field.append(sorted(i, key=a))
print(new_field)
Output:
[[['PEASH', 5], ['ZOMB', 15], None, None, None, None, None],
[['PEASH', 5], ['ZOMB', 15], None, None, None, None, None],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None]]