Need help converting a list of strings to a dictionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zabombafor
    New Member
    • Nov 2011
    • 1

    Need help converting a list of strings to a dictionary

    So I have a list of strings that I would like to convert to a dictionary. I want the keys in the dictionary to be the names of the strings and the... other part... i forget what its called, the part after the keys, to be how many times the string occures, this is part of a larger program so I won't submit all the code I have so far, but the list I want to convert is the following (There are a couple other lists aswell but I figure I can probably just change the code to fit them):

    s2ID = ['a01', 'a02', 'a02', 'a02', 'a03', 'a03', 'a03', 'a03']

    I won't know how many elements will be in the list in the final version and these are just values i've subbed in so that I have something to work with, so keep in mind that I won't necissarily have one a01 and 2 a02's in the final version, i could also have a75 for all i know, all i know is that they will follow the format of a(int)(int), not sure if this affects the code or not but just in case

    Any help would be greatly appreciated as would an explanation (even if its a quick and simple one) of any code given. Thanks in advance for the help!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Initialize an empty dictionary
    Iterate on the list
    If the dictionary does not contain the string, assign the string as key of the dictionary with a value of one
    If the dictionary contains the current string, increment the value by one

    Built-in functions, methods and keywords that can be used to accomplish the above:
    for
    if
    in
    dictionary methods has_key(), setdefault(), or get()
    Last edited by bvdet; Nov 1 '11, 09:11 PM.

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      Sounds like a text book example for default dictionary!

      See here.

      Below is an interactive session:

      Code:
      >>> from collections import defaultdict
      >>> d=defaultdict(int)
      >>> s2ID = ['a01', 'a02', 'a02', 'a02', 'a03', 'a03', 'a03', 'a03']
      >>> for k in s2ID:
      	    d[k]+=1
      >>> d
      defaultdict(<type 'int'>, {'a02': 3, 'a03': 4, 'a01': 1})
      >>>
      Setting the int in defaultdict means that if the item mentioned doesn't exist, you simply make one with an integer (valued 0).

      Comment

      • papin
        New Member
        • Nov 2011
        • 7

        #4
        Also:
        Code:
        >>> import operator
        >>> keys, d = range(len(s2ID)), {}
        >>> map(operator.setitem, [d]*len(keys), keys, s2ID)
        >>> print d
        {0: 'a01', 1: 'a02', 2: 'a02', 3: 'a02', 4: 'a03', 5: 'a03', 6: 'a03', 7: 'a03'}

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          I prefer this way:
          Code:
          >>> s2ID = ['a01', 'a02', 'a02', 'a02', 'a03', 'a03', 'a03', 'a03']
          >>> dd = {}
          >>> for s in s2ID:
          ... 	v = dd.setdefault(s, 0)
          ... 	dd[s] = v+1
          ... 	
          >>> dd
          {'a02': 3, 'a03': 4, 'a01': 1}
          >>>

          Comment

          • papin
            New Member
            • Nov 2011
            • 7

            #6
            @bvdet
            I thought it was interesting to provide another tip on how create a dict from a list (given the list be the values).

            Your solution is perfectly right.

            To get the same answer, I'll do:
            Code:
            >>> d = dict(zip(set(s2ID),  map(lambda x: s2ID.count(x), set(s2ID))))
            >>> print d
            {'a02': 3, 'a03': 4, 'a01': 1}
            Actually, I don't use list.setdefault () because I never learned how to use it. In fact, my code may be less efficient than yours.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              @papin

              It is interesting to see other's solutions. Thanks for your contribution.

              Comment

              Working...