I need a string/list/generator/comprehension incantation.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven W. Orr

    I need a string/list/generator/comprehension incantation.

    I really tried. I give up.

    I got this one last time (for which I'm very grateful).

    import calendar
    months = dict([(month,ii) for ii,month in enumerate(calen dar.month_abbr)][1:])

    Now I want something that's going to give me a string whose value is the
    set of all of the first letters of months. Order is not important.

    And for extra credit, I need the string whose value is the set of all of
    the letters of months minus the first letter.

    E.g., 'ADFJMONS', 'acbeglonprutvy '

    Thanks in advance to all the wizards out there. :-)

    --
    Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
    happened but none stranger than this. Does your driver's license say Organ ..0
    Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
    individuals! What if this weren't a hypothetical question?
    steveo at syslang.net
  • Jon Ribbens

    #2
    Re: I need a string/list/generator/comprehension incantation.

    In article <mailman.6789.1 177087364.32031 .python-list@python.org >, Steven W. Orr wrote:
    Now I want something that's going to give me a string whose value is the
    set of all of the first letters of months. Order is not important.
    "".join(set (m[0] for m in calendar.month_ abbr[1:]))
    And for extra credit, I need the string whose value is the set of all of
    the letters of months minus the first letter.
    "".join(set("". join(m[1:] for m in calendar.month_ abbr[1:])))

    Those are the ways that are obvious to me anyway, maybe there are
    better ways. If you actually want a set not a string, remove the
    outer-most "".join().

    Comment

    • Scott David Daniels

      #3
      Re: I need a string/list/generator/comprehension incantation.

      Steven W. Orr wrote:
      I really tried. I give up.
      >
      I got this one last time (for which I'm very grateful).
      ... Now I want something that's going to give me a string whose value is the
      set of all of the first letters of months. Order is not important.
      >
      And for extra credit, I need the string whose value is the set of all of
      the letters of months minus the first letter.
      Boy am I sorry we did your homework for you. If you have no idea where
      to go, and you can't even show your work, you need to see a TA or
      instructor. You are already in over your head; back up and learn to
      swim.

      --
      --Scott David Daniels
      scott.daniels@a cm.org

      Comment

      Working...