I am new to stack overflow so please excuse me if I am not clear.
aim: to write a program that would remove the elements that are present in preceding nested lists
here is the list:
gang=[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 6, 9, 12, 15], [6, 12], [5, 10, 15]]
desired output: gang=[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 6, 9, 15], [5, 10]]
attempt:
for i in range(1,len(gang)):
elem=gang[i]
for j in range(len(elem)):
for hey in range(i):
if elem[j] in gang[hey]:
elem.pop(j)
explanation of the attempt:
1)to traverse through list gang, I used a for loop (range is 1,len(gang) bcs 0th element wont be changed as it is the first one in the list)
2)I have declared a variable, 'elem' which equates to gang[i] i.e. the elements of gang from gang[1] to gang[-1]
3)Now, to traverse through the elements of the elements i.e. the elements of each nested list, I have used another for loop
4)the last for loop (for hey in range(i)) is used so that I can confirm if elem[j] i.e. element of nested list, exists in preceding nested lists, and if the condition is satisfied elem[j] would be removed
expected output:
gang=[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 6, 9, 15], [5, 10]]
output:
if elem[j] in gang[hey]:
IndexError: list index out of range
question: why is this error showing? potential fix for it? any better way to achieve aim?
-
Please go through the intro tour, the help center and how to ask a good question to see how this site works and to help you improve your current and future questions, which can help you get better answers. "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a specific question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation.Prune– Prune2021年03月13日 05:45:39 +00:00Commented Mar 13, 2021 at 5:45
-
check this stackoverflow.com/questions/15037226/…Chathuranga– Chathuranga2021年03月13日 05:47:42 +00:00Commented Mar 13, 2021 at 5:47
-
thanks for advice, I have added my attempt at the problemCaptian Pool– Captian Pool2021年03月13日 05:48:04 +00:00Commented Mar 13, 2021 at 5:48
-
check this out : rextester.com/IVOR40775A l w a y s S u n n y– A l w a y s S u n n y2021年03月13日 05:49:03 +00:00Commented Mar 13, 2021 at 5:49
-
actually I want it so that elements don't repeatCaptian Pool– Captian Pool2021年03月13日 05:50:56 +00:00Commented Mar 13, 2021 at 5:50
2 Answers 2
Here it is. You create a visited list, iterate backward through each sublist, and remove an element if it is already visited:
gang=[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 6, 9, 12, 15], [6, 12], [5, 10, 15]]
visited = []
for lst in gang:
for i in range(len(lst)-1, -1, -1):
if lst[i] not in visited:
visited.append(lst[i])
else:
lst.pop(i)
print(gang)
#[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 9, 15], [], [5]]
Comments
The library itertools is very helpful in this case
import itertools
gang=[[1], [7], [11], [13], [2, 4, 6, 8, 10, 12, 14], [3, 6, 9, 12, 15], [6, 12], [5, 10, 15]]
gang.sort()#Sorting the list
gang = list(k for k,_ in itertools.groupby(gang))#This removes repetition
print (gang)
Check it on a repl here https://repl.it/join/keztvujw-siddharthagraw2