dict generator question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Mullis

    dict generator question

    Hi,

    Let's say I have an arbitrary list of minor software versions of an
    imaginary software product:

    l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]

    I'd like to create a dict with major_version : count.

    (So, in this case:

    dict_of_counts = { "1.1" : "1",
    "1.2" : "2",
    "1.3" : "2" }

    Something like:

    dict_of_counts = dict([(v[0:3], "count") for v in l])

    I can't seem to figure out how to get "count", as I cannot do x += 1
    or x++ as x may or may not yet exist, and I haven't found a way to
    create default values.

    I'm most probably not thinking pythonically enough... (I know I could
    do this pretty easily with a couple more lines, but I'd like to
    understand if there's a way to use a dict generator for this).

    Thanks in advance

    SM


    --
    Simon Mullis
  • marek.rocki@wp.pl

    #2
    Re: dict generator question

    Simon Mullis napisa³(a):
    Something like:
    >
    dict_of_counts = dict([(v[0:3], "count") for v in l])
    >
    I can't seem to figure out how to get "count", as I cannot do x += 1
    or x++ as x may or may not yet exist, and I haven't found a way to
    create default values.
    It seems to me that the "count" you're looking for is the number of
    elements from l whose first 3 characters are the same as the v[0:3]
    thing. So you may try:
    >>dict_of_count s = dict((v[0:3], sum(1 for x in l if x[:3] == v[:3])) for v in l)
    But this isn't particularly efficient. The 'canonical way' to
    construct such histograms/frequency counts in python is probably by
    using defaultdict:
    >>dict_of_count s = collections.def aultdict(int)
    >>for x in l:
    >> dict_of_counts[x[:3]] += 1
    Regards,
    Marek

    Comment

    • pruebauno@latinmail.com

      #3
      Re: dict generator question

      On Sep 18, 10:54 am, "Simon Mullis" <si...@mullis.c o.ukwrote:
      Hi,
      >
      Let's say I have an arbitrary list of minor software versions of an
      imaginary software product:
      >
      l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
      >
      I'd like to create a dict with major_version : count.
      >
      (So, in this case:
      >
      dict_of_counts = { "1.1" : "1",
      "1.2" : "2",
      "1.3" : "2" }
      >
      Something like:
      >
      dict_of_counts = dict([(v[0:3], "count") for v in l])
      >
      I can't seem to figure out how to get "count", as I cannot do x += 1
      or x++ as x may or may not yet exist, and I haven't found a way to
      create default values.
      >
      I'm most probably not thinking pythonically enough... (I know I could
      do this pretty easily with a couple more lines, but I'd like to
      understand if there's a way to use a dict generator for this).
      >
      Thanks in advance
      >
      SM
      >
      --
      Simon Mullis
      3 lines:

      from collections import defaultdict
      dd=defaultdict( int)
      for x in l: dd[x[0:3]]+=1

      Comment

      • George Sakkis

        #4
        Re: dict generator question

        On Sep 18, 10:54 am, "Simon Mullis" <si...@mullis.c o.ukwrote:
        Hi,
        >
        Let's say I have an arbitrary list of minor software versions of an
        imaginary software product:
        >
        l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
        >
        I'd like to create a dict with major_version : count.
        >
        (So, in this case:
        >
        dict_of_counts = { "1.1" : "1",
        "1.2" : "2",
        "1.3" : "2" }
        >
        Something like:
        >
        dict_of_counts = dict([(v[0:3], "count") for v in l])
        >
        I can't seem to figure out how to get "count", as I cannot do x += 1
        or x++ as x may or may not yet exist, and I haven't found a way to
        create default values.
        >
        I'm most probably not thinking pythonically enough... (I know I could
        do this pretty easily with a couple more lines, but I'd like to
        understand if there's a way to use a dict generator for this).
        Not everything has to be a one-liner; also v[0:3] is wrong if any sub-
        version is greater than 9. Here's a standard idiom (in 2.5+ at least):

        from collection import defaultdict
        versions = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]

        major2count = defaultdict(int )
        for v in versions:
        major2count['.'.join(v.spli t('.',2)[:2])] += 1
        print major2count

        HTH,
        George

        Comment

        • pruebauno@latinmail.com

          #5
          Re: dict generator question

          On Sep 18, 10:54 am, "Simon Mullis" <si...@mullis.c o.ukwrote:
          Hi,
          >
          Let's say I have an arbitrary list of minor software versions of an
          imaginary software product:
          >
          l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
          >
          I'd like to create a dict with major_version : count.
          >
          (So, in this case:
          >
          dict_of_counts = { "1.1" : "1",
          "1.2" : "2",
          "1.3" : "2" }
          >
          Something like:
          >
          dict_of_counts = dict([(v[0:3], "count") for v in l])
          >
          I can't seem to figure out how to get "count", as I cannot do x += 1
          or x++ as x may or may not yet exist, and I haven't found a way to
          create default values.
          >
          I'm most probably not thinking pythonically enough... (I know I could
          do this pretty easily with a couple more lines, but I'd like to
          understand if there's a way to use a dict generator for this).
          >
          Thanks in advance
          >
          SM
          >
          --
          Simon Mullis
          Considering 3 identical "simultpost " solutions I'd say:
          "one obvious way to do it" FTW :-)

          Comment

          • Simon Mullis

            #6
            Re: dict generator question

            Haha!

            Thanks for all of the suggestions... (I love this list!)

            SM

            2008/9/18 <pruebauno@lati nmail.com>:
            On Sep 18, 10:54 am, "Simon Mullis" <si...@mullis.c o.ukwrote:
            >Hi,
            >>
            >Let's say I have an arbitrary list of minor software versions of an
            >imaginary software product:
            >>
            >l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
            >>
            >I'd like to create a dict with major_version : count.
            >>
            >(So, in this case:
            >>
            >dict_of_coun ts = { "1.1" : "1",
            > "1.2" : "2",
            > "1.3" : "2" }
            >>
            >Something like:
            >>
            >dict_of_coun ts = dict([(v[0:3], "count") for v in l])
            >>
            >I can't seem to figure out how to get "count", as I cannot do x += 1
            >or x++ as x may or may not yet exist, and I haven't found a way to
            >create default values.
            >>
            >I'm most probably not thinking pythonically enough... (I know I could
            >do this pretty easily with a couple more lines, but I'd like to
            >understand if there's a way to use a dict generator for this).
            >>
            >Thanks in advance
            >>
            >SM
            >>
            >--
            >Simon Mullis
            >
            Considering 3 identical "simultpost " solutions I'd say:
            "one obvious way to do it" FTW :-)
            --

            >


            --
            Simon Mullis
            _______________ __
            simon@mullis.co .uk

            Comment

            • Paul Rubin

              #7
              Re: dict generator question

              "Simon Mullis" <simon@mullis.c o.ukwrites:
              l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
              ...
              dict_of_counts = dict([(v[0:3], "count") for v in l])
              Untested:

              def major_version(v ersion_string):
              "convert '1.2.3.2' to '1.2'"
              return '.'.join(versio n_string.split( '.')[:2])

              dict_of_counts = defaultdict(int )

              for x in l:
              dict_of_counts[major_version(l )] += 1

              Comment

              Working...