The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Deleting specific lists in python
Discuss Deleting specific lists in python in the Python Programming forum on Dev Shed. Deleting specific lists in python Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 10th, 2013, 05:02 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 1 h 44 m 14 sec
Reputation Power: 0
|
|
|
Deleting specific lists in python
Hi everyone
superlist=[[0,8],[1],[2],[3,6],[4],[5],[6,3,6],[7],[8,0,8],[9]]
I am trying to create a function on a list of lists that will do the following:
1) Deletes a repeat list( i.e. deletes a list that contains a number that has already been used in another list) from left to right so in the superlist (see above) it will identify that the first list contains a '0' and an '8' so any further list containing any of those two elements will be deleted. Therefore the penultimate list will be deleted.
So for the example above, if the function were called reduce() then, reduce(superlist) = [[0,8],[1],[2],[3,6],[4],[5],[7],[9]]
Thank you
|

January 10th, 2013, 08:57 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 75
  
Time spent in forums: 1 Day 3 h 5 m 57 sec
Reputation Power: 2
|
|
|
This shouldn't be too hard to write, but your specification is a little vague as of now and needs to be clarified. In particular, the following questions came up for me when trying to implement this:
1. If one of the lists is deleted, should the numbers in that list still be added to the set of numbers seen? (For instance, given the list [[1], [1, 2], [2]], should the third entry be deleted?)
2. Is this meant to mutate the list in place, or create a new list?
I would also suggest against naming your function reduce, as there is a built-in function by that name in Python 2 (moved to the functools module in Python 3) that does something completely different.
|

January 11th, 2013, 06:10 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 1 h 44 m 14 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Nyktos This shouldn't be too hard to write, but your specification is a little vague as of now and needs to be clarified. In particular, the following questions came up for me when trying to implement this:
1. If one of the lists is deleted, should the numbers in that list still be added to the set of numbers seen? (For instance, given the list [[1], [1, 2], [2]], should the third entry be deleted?)
2. Is this meant to mutate the list in place, or create a new list?
I would also suggest against naming your function reduce, as there is a built-in function by that name in Python 2 (moved to the functools module in Python 3) that does something completely different. |
Thanks.To answer the second question - i want to mutate it. To answer your first question, with the type of lists I will be working on it doesnt matter, because the repeated lists have all the numbers repeated.
An example of a list I will be working with:
[[1,2,3],[4],[2,2,3,1],[4], [5,8,9],[8,9,5,5,9]]
after the function is applied it will turn into:
[[1,2,3],[4],[5,8,9]]
Hope thats made it clearer.
Sam
|

January 11th, 2013, 03:04 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 75
  
Time spent in forums: 1 Day 3 h 5 m 57 sec
Reputation Power: 2
|
|
Code:
def delete_duplicates(list_of_lists):
numbers_seen = set()
for idx, L in enumerate(list_of_lists):
if any(item in numbers_seen for item in L):
del list_of_lists[idx]
else:
numbers_seen.update(L)
Code:
>>> L = [[0, 8], [1], [2], [3, 6], [4], [5], [6, 3, 6], [7], [8, 0, 8], [9]]
>>> delete_duplicates(L)
>>> L
[[0, 8], [1], [2], [3, 6], [4], [5], [7], [9]]
|

January 12th, 2013, 10:38 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 1 h 44 m 14 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Nyktos
Code:
def delete_duplicates(list_of_lists):
numbers_seen = set()
for idx, L in enumerate(list_of_lists):
if any(item in numbers_seen for item in L):
del list_of_lists[idx]
else:
numbers_seen.update(L)
Code:
>>> L = [[0, 8], [1], [2], [3, 6], [4], [5], [6, 3, 6], [7], [8, 0, 8], [9]]
>>> delete_duplicates(L)
>>> L
[[0, 8], [1], [2], [3, 6], [4], [5], [7], [9]]
|
Thanks a lot Nyktos 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|