#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep 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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    4
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    4
    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]]
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    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

IMN logo majestic logo threadwatch logo seochat tools logo