I'm trying to remove duplicate items from a multidimensional list. My goal is to remove items that are the same across the lists.
For example: List 2, List 6 and List 7 contains the car Bentley. I want to remove that car from 2 of the lists.
How do I accomplish this?
The code below only works if I pass in a single list containing duplicate entries, but I need to deduplicate a multidimensional list.
cars = [
["Acura", "Alfa Romeo", "Aston Martin", "Audi", "Aston Martin"],
["Bentley", "BMW", "Bugatti", "Buick"],
["Cadillac", "Chrysler", "Citroen"],
["Dodge", "Ferrari", "Fiat", "Ford"],
["Geely", "Honda", "Hyundai", "Infiniti"],
["Alfa Romeo", "Bentley", "Hyundai", "Lamborghini"],
["Koenigsegg", "Bentley", "Maserati", "Lamborghini"]
]
def remove(duplicate):
final_list = []
for num in duplicate:
if num not in final_list:
final_list.append(num)
return final_list
print (remove(cars))
returns:
[
['Acura', 'Alfa Romeo', 'Aston Martin', 'Audi','Aston Martin']
['Bentley', 'BMW', 'Bugatti', 'Buick'],
['Cadillac', 'Chrysler', 'Citroen'],
['Dodge', 'Ferrari', 'Fiat', 'Ford'],
['Geely', 'Honda', 'Hyundai', 'Infiniti'],
['Alfa Romeo', 'Bentley', 'Hyundai', 'Lamborghini'
['Koenigsegg', 'Bentley', 'Maserati', 'Lamborghini']
]
My desired output after deduplication is shown below. No list within this multidimensional list contains a duplicate entry.
[
['Acura', 'Alfa Romeo', 'Aston Martin', 'Audi']
['Bentley', 'BMW', 'Bugatti', 'Buick'],
['Cadillac', 'Chrysler', 'Citroen'],
['Dodge', 'Ferrari', 'Fiat', 'Ford'],
['Geely', 'Honda', 'Hyundai', 'Infiniti'],
['Bentley', 'Hyundai', 'Lamborghini'
['Koenigsegg', 'Maserati']
]
You can do it like this:
def remove(duplicate):
final_list = []
found = set([])
for num in duplicate:
lst = []
for element in num:
if element not in found:
found.add(element)
lst.append(element)
final_list.append(lst)
return final_list
Output
[
['Acura', 'Alfa Romeo', 'Aston Martin', 'Audi'],
['Bentley', 'BMW', 'Bugatti', 'Buick'],
['Cadillac', 'Chrysler', 'Citroen'],
['Dodge', 'Ferrari', 'Fiat', 'Ford'],
['Geely', 'Honda', 'Hyundai', 'Infiniti'],
['Lamborghini'],
['Koenigsegg', 'Maserati']
]
You can use memoization with such purpose. Store the first occurrence of every element in a list lets say list F . If an element is not in F, then it is unique . Store the unique element in F and and repeat the process.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.