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

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Unhappy Dictionary manipulation


    Hi am a newbie in python, any help to this question will be appreciated.

    By using a dict, write a method only_once(a) that takes a list, a, as an argument and returns a list containing the elements of a that occur exactly once. elements should appear in the same order as their first occurrence in a.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    2
    Originally Posted by molley
    Hi am a newbie in python, any help to this question will be appreciated.

    By using a dict, write a method only_once(a) that takes a list, a, as an argument and returns a list containing the elements of a that occur exactly once. elements should appear in the same order as their first occurrence in a.
    It's really much better form if you at least try something first.
    Let's say you create a dictionary: d={}
    You will use the"get(key,default)" method to assign values to the key for each element of "a".

    Note, I would actually use set objects to do this but I gather your assignment requires a dictionary.

    So loop through the list, a, and increment the value in the dictionary:
    Code:
    for i in a:
        d[i]=d.get(i,0)+1
    Then to create a list, b, containing elements that only occur in a once:
    Code:
    b=[i for i in d if d[i]==1]

IMN logo majestic logo threadwatch logo seochat tools logo