Better way to do this?

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

    Better way to do this?

    Hi folks,

    I have a tuple of tuples, in the form--((code1, 'string1'),(cod e2,
    'string2'),(cod e3, 'string3'),)

    Codes are unique. A dict would probably be the best approach but this
    is beyond my control.

    Here is an example:
    >>>pets = ((0,'cat'),(1,' dog'),(2,'mouse '))
    If I am given a value for the code I need to retrieve the string
    representation. The value is guaranteed to be valid.

    This is what I came up with...
    >>>value=1
    >>>[ pet for code, pet in pets if value==code ][0]
    'dog'

    It does the job, I was just curious if there was a better way to do
    it.



  • imho

    #2
    Re: Better way to do this?

    PRC ha scritto:
    Hi folks,
    >
    I have a tuple of tuples, in the form--((code1, 'string1'),(cod e2,
    'string2'),(cod e3, 'string3'),)
    >
    Codes are unique. A dict would probably be the best approach but this
    is beyond my control.
    >
    Here is an example:
    >>>pets = ((0,'cat'),(1,' dog'),(2,'mouse '))
    >
    If I am given a value for the code I need to retrieve the string
    representation. The value is guaranteed to be valid.
    >
    This is what I came up with...
    >
    >>>value=1
    >>>[ pet for code, pet in pets if value==code ][0]
    'dog'
    >
    It does the job, I was just curious if there was a better way to do
    it.
    Can't You first convert the tuple of tuples in a dict, and then
    retrieving the value given a code value ?
    >>dct = dict(pets)
    >>dct[1]
    'dog'


    Comment

    • Tim Chase

      #3
      Re: Better way to do this?

      I have a tuple of tuples, in the form--((code1, 'string1'),(cod e2,
      'string2'),(cod e3, 'string3'),)
      >
      Codes are unique. A dict would probably be the best approach but this
      is beyond my control.
      >
      Here is an example:
      >>>pets = ((0,'cat'),(1,' dog'),(2,'mouse '))
      >
      If I am given a value for the code I need to retrieve the string
      representation. The value is guaranteed to be valid.
      >
      This is what I came up with...
      >
      >>>value=1
      >>>[ pet for code, pet in pets if value==code ][0]
      'dog'
      Does anything prevent you from writing that as
      >>dict(pets)[value]
      'dog'

      ?

      -tkc



      Comment

      • Carl Banks

        #4
        Re: Better way to do this?

        On Feb 11, 3:57 pm, imho <ce...@comeno.i twrote:
        PRC ha scritto:
        >
        >
        >
        Hi folks,
        >
        I have a tuple of tuples, in the form--((code1, 'string1'),(cod e2,
        'string2'),(cod e3, 'string3'),)
        >
        Codes are unique. A dict would probably be the best approach but this
        is beyond my control.
        >
        Here is an example:
        >>pets = ((0,'cat'),(1,' dog'),(2,'mouse '))
        >
        If I am given a value for the code I need to retrieve the string
        representation. The value is guaranteed to be valid.
        >
        This is what I came up with...
        >
        >>value=1
        >>[ pet for code, pet in pets if value==code ][0]
        'dog'
        >
        It does the job, I was just curious if there was a better way to do
        it.
        A listcomp will traverse the whole tuple even if it finds the code
        early in the tuple. It's better just use a for loop and break out as
        soon as you find it.

        for code,pet in pets:
        if value == code:
        break
        else:
        raise ValueError("cod e %s not found" % value)

        As an added bonus, you get a more useful error message if it's not
        there (I know what you said, but bugs happen). If it's too bulky for
        you, put it into its own function and call it.

        Can't You first convert the tuple of tuples in a dict, and then
        retrieving the value given a code value ?
        >
        >>dct = dict(pets)
        >>dct[1]
        'dog'
        A linear search might be faster than building a dict if this search is
        only to be done once for a given tuple.


        Carl Banks

        Comment

        Working...