473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Better way to do this?

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:
>>>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.

Feb 11 '08 #1
3 953
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:
>>>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'
Feb 11 '08 #2
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:
>>>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

Feb 11 '08 #3
On Feb 11, 3:57 pm, imho <ce...@comeno.itwrote:
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:
>>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("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
Feb 11 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: ForHimself Every Man | last post by:
What's better about Rattlesnakes than Python. I'm sure there's something. What is it? This is not a troll. I'm a snake shooping and I want people's answers. I don't know beans about...
10
by: michael newport | last post by:
Dear friends of database(s), After 13 years of Ingres, I am now using Oracle. But is Oracle technically better than Ingres. I would be much obliged if anyone could shed some light on the...
24
by: Faith Dorell | last post by:
I really donīt like C.You can write better programs in BASIC than in C, if you donīt like this language. I donīt understand how C became so popular, although much better programming languages...
39
by: bazad | last post by:
Hi, I am not using C all the time. I have a general understanding of C and nothing else. The recent reply to use strlcpy and strlcat showed me that I am not aware of the best and safe...
2
by: monkeydragon | last post by:
Which is better, using ReadFile/WriteFile or use fstream?
33
by: Protoman | last post by:
Which is better for general-purpose programming, C or C++? My friend says C++, but I'm not sure. Please enlighten me. Thanks!!!!!
22
by: JoeC | last post by:
I am working on another game project and it is comming along. It is an improvment over a previous version I wrote. I am trying to write better programs and often wonder how to get better at...
21
by: gavino | last post by:
why?
3
by: Ryan Liu | last post by:
Hi, Is Async I/O (e.g. NetworkStream.Begin/End Read/Write) always better than synchronous I/O? At least as good? When I don't concern about easy or difficult to write code, should I always...
43
by: Pawel_Iks | last post by:
I've read somewhere that c++ is something more than better c ... then I talk with my friend and he claimed that c++ is nothing more than better c ... I tried to explain him that he was wrong but I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.