Better way to do this?

P

PRC

Hi folks,

I have a tuple of tuples, in the form--> ((code1, 'string1'),(code2,
'string2'),(code3, 'string3'),)

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

Here is an example:
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.
 
D

Diez B. Roggisch

PRC said:
Hi folks,

I have a tuple of tuples, in the form--> ((code1, 'string1'),(code2,
'string2'),(code3, 'string3'),)

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

Here is an example:
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.

This is linear in the number of elements. It can get better (log_2 n) if
you can rely on the order of codes as in your given example - then you
should be able to use the module bisect to find the index quicker.

Diez
 
I

imho

PRC ha scritto:
Hi folks,

I have a tuple of tuples, in the form--> ((code1, 'string1'),(code2,
'string2'),(code3, 'string3'),)

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

Here is an example:
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'
 
T

Tim Chase

I have a tuple of tuples, in the form--> ((code1, 'string1'),(code2,
'string2'),(code3, 'string3'),)

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

Here is an example:
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
'dog'

?

-tkc
 
C

Carl Banks

PRC ha scritto:


Hi folks,
I have a tuple of tuples, in the form--> ((code1, 'string1'),(code2,
'string2'),(code3, 'string3'),)
Codes are unique. A dict would probably be the best approach but this
is beyond my control.
Here is an example:
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("code %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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top