Hello I’m trying to sort through a nested list and remove sublists based on its [0][0][0] index. I can get it to work if the rest of the list is also identical, however, with the first index ([0][0][0]) the same and the rest of the sublist ([0][0][1] for instance) is different to the constant, it no longer deletes. (the code makes more sense than I do…). I’m sure I’m missing something simple, but I can’t quite find it.
c = [[['1100', ['1100', 'other text', 'a', '1', '2']],
['1101', ['1200', 'other text', 'a', '1', '2']],
['1200', ['1160', 'other text', 'a', '1', '2']],
['1201', ['1110', 'other text', 'a', '1', '2']],
['1211', ['1230', 'other text', 'a', '1', '2']],
['1100', ['4200', 'other text', 'a', '1', '2']],
['1200', ['1100', 'other text', 'a', '1', '2']],
['1235', ['1500', 'other text', 'a', '1', '2']]]]
j = 0
for i in c[0]:
list_len = len(c[0])
print ("The current list length is", list_len)
print ("Searching list for", "'" + c[0][j][0] + "'")
for i in c[0][j]:
occur_list = []
jtemp = int(j)
print ("'jtemp' currently set to", jtemp)
while jtemp < list_len - 1:
occur = c[0][jtemp].count(c[0][j][0])
jtemp = jtemp + 1
print("Checked index", "[" + str(jtemp) + "]")
occur_list.append(occur)
occur = sum(occur_list)
print ("'" + c[0][j][0] + "'","occurs", occur, "time(s)")
print ("'jtemp' currently set to", jtemp)
if occur > 1:
c[0].reverse()
removed = c[0].pop(jtemp)
c[0].reverse()
print("Removed", removed)
else:
continue
if j < list_len:
j = j + 1
else:
continue
for x in c[0]:
print(x)
The desired output should be:
['1100', ['1100', 'other text', 'a', '1', '2']]
['1101', ['1200', 'other text', 'a', '1', '2']]
['1200', ['1160', 'other text', 'a', '1', '2']]
['1201', ['1110', 'other text', 'a', '1', '2']]
['1211', ['1230', 'other text', 'a', '1', '2']]
['1235', ['1500', 'other text', 'a', '1', '2']]
Removing:
['1100', ['4200', 'other text', 'a', '1', '2']]
['1200', ['1100', 'other text', 'a', '1', '2']]
because they are the second occurance of 1100 and 1200 in the first index respectively.
Currently the code only removes the first two.
Also note: I’ve set heaps of print commands to track my progress, but they certainly are not essential and will not be in the final code.