pythonlistalgorithmarraylistoutput

# Is there a way to avoid producing nested lists when allocating lists?

Here's a git link if you want to see the full code/run the program for yourself.

The summary of my problem is: I have a nested list, and lists inside that list should move to another list which they do. Those lists that are moved end up being triple nested for some reason like this: [[[66, 81]], [[83, 41]]] instead of this: [ ... [83,41], [66,81] ]

That I made a program that follows a predictive algorithm RITMO. The program has Nodes/Drones that complete tasks: (moving to points).

This randomly generates a list of (x,y) values. It's messy but it still works. (pointgen)

``````randomList = []
randomList1 = []
pointlist = []
for i in range(1000):  #This is overkill and could be accomplished with neater code but it works.
r=random.randint(1,100)
r1=random.randint(1,100)
if r not in randomList:
randomList.append(r)
if r1 not in randomList1:
randomList1.append(r1)
while len(randomList1) > numofp:
randomList1.pop()
while len(randomList) > numofp:
randomList.pop()

for i in range(0, len(randomList)):
insidelist = []
insidelist.append(randomList[i]) #X
insidelist.append(randomList1[i]) #Y
pointlist.append(insidelist) #creats massive point list.

return pointlist

``````

This takes a the list from above and randomly breaks it into (min_length & max_length) 3-7 segments. (pointilist)

``````result = []

while input_list:
sublist_length = random.randint(min_length, max_length) #Gen random num
if sublist_length > len(input_list):
sublist_length = len(input_list)

selected_elements = input_list[:sublist_length]
result.append(selected_elements) #Splice section
input_list = input_list[sublist_length:]

return result
``````
``````    newPointlist = break_list_into_sublists(oldPointlist, 3, 7)

for i in range(0, AmountofNodes): #List of 5 Nodes/Drones randomly given name & points
Nodelist.append(Node(name_gen(), newPointlist[i - 1], 5)) # 5 is queue/task max
``````

Output: You can see how lists in tasks are moved to the positions list

1. Name: sJ\Q# Current tasks: 2 lists of tasks: [[[66, 81]], [[83, 41]]] queue size threshold: 5 positions: [[77, 49], [48, 59], [71, 35], [67, 85]]

1. Name:sJ\Q# Current tasks: 1 list of tasks: [[[66, 81]]] queue size threshold: 5 positions: [[77, 49], [48, 59], [71, 35], [67, 85], [[83, 41]]]

The problem is I have no idea where "[[[66, 81]], [[83, 41]]]" triple nested lists or elements are coming from. They should just be [ ... [83,41], [66,81] ]

It might be an error in how the Node object moves tasks from different nodes that results in unnecessary nesting Here's the link to Git there are only 3 files: main, droneobject , randomnodenamgen

If anyone could help me out that would be great!!

Solution

• It looks to me like your second excerpt is causing the nesting:

``````selected_elements = input_list[:sublist_length]
result.append(selected_elements) #Splice section
input_list = input_list[sublist_length:]
``````

`input_list` is a list of 2-element lists. So is `selected_elements`. `result` starts as an empty list and has lists of lists appended to it, making it 3 lists deep.
If you rewrite it...

``````selected_elements = input_list[:sublist_length]
result = result + selected_elements #Splice section
input_list = input_list[sublist_length:]
``````

... it should only be 2 lists deep.