Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 10th, 2013, 05:02 PM
Sam Hunter Sam Hunter is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 Sam Hunter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old January 10th, 2013, 08:57 PM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 75 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #3  
Old January 11th, 2013, 06:10 AM
Sam Hunter Sam Hunter is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 Sam Hunter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old January 11th, 2013, 03:04 PM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 75 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
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]]

Reply With Quote
  #5  
Old January 12th, 2013, 10:38 AM
Sam Hunter Sam Hunter is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 Sam Hunter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Deleting specific lists in python

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap