Using tuples to eliminate multiple dict values

B

Benshep

I need a dictionary that returns the same value for multiple keys.

i.e.

(1) >>> dict = { (1,2,3) : 'text' , (5,6,7) : 'other text' }
(2) >>>dict[1]
(3) 'text'

I cant figure out what i need on line 2 to make this scenario work. Is
there a simple way to check if the a number is present in the key and
then return the value?

Thanks
 
P

Prasad, Ramit

-----Original Message-----
From: [email protected] [mailto:p[email protected]] On Behalf Of Benshep
Sent: Friday, September 16, 2011 1:48 PM
To: (e-mail address removed)
Subject: Using tuples to eliminate multiple dict values

I need a dictionary that returns the same value for multiple keys.

i.e.

(1) >>> dict = { (1,2,3) : 'text' , (5,6,7) : 'other text' }
(2) >>>dict[1]
(3) 'text'

I cant figure out what i need on line 2 to make this scenario work. Is
there a simple way to check if the a number is present in the key and
then return the value?

Thanks
----------------------------------------------------------------

I think I must be missing something. Can you not do the following?

d = { 1:'text', 2:'text', 3:'text', 5:'other text', 6:'other text', 7:'other text' }
d[1]
'text'



If you are they need to share the same value you can also do something like this.

t1 = [ 'text' ]
t2 = [ 'other text' ]
d = { 1:t1, 2:t1, 3:t1, 5:t2, 6:t2, 7:t2 }
d[1]
['text']
d[1][0]='new text'
d[2][0]
'newtext'
d
{1: ['new text'], 2: ['new text'], 3: ['new text'], 5: ['other text'], 6: ['other text'], 7: ['other text']}


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
 
M

MRAB

I need a dictionary that returns the same value for multiple keys.

i.e.

(1)>>> dict = { (1,2,3) : 'text' , (5,6,7) : 'other text' }

That will create a dict with 2 keys, both of which are tuples.
(2)>>>dict[1]
(3) 'text'

I cant figure out what i need on line 2 to make this scenario work. Is
there a simple way to check if the a number is present in the key and
then return the value?
If you want multiple keys, then you must provide them separately:

my_dict = {1: 'text', 2: 'text', 3: 'text', 5: 'other text', 6 : 'other
text', 7: 'other text'}

or use 'for' to iterate through the keys for each value:

my_dict = dict([(k, 'text') for k in (1, 2, 3)] + [(k, 'other text') for
k in (5, 6, 7)])

or something similar.
 
B

Benshep

thanks,

I had a feeling that may be the way to go and my data just changed so
it will work better that way.

Thanks for the quick reply
 
C

Chris Angelico

(1)   >>> dict = { (1,2,3) : 'text' , (5,6,7) : 'other text' }

As I understand it, you're looking for a simple syntax for what MRAB posted:
my_dict = {1: 'text', 2: 'text', 3: 'text', 5: 'other text', 6 : 'othertext', 7: 'other text'}

Since your first line of code is syntactically legal, you could do a
simple check afterwards to translate it:

my_dict = { (1,2,3) : 'text' , (5,6,7) : 'other text' }
for k in list(my_dict.keys()):
if isinstance(k,tuple):
val=my_dict.pop(k)
for i in k: my_dict=val

Tested in Python 3.2; in Python 2, the list() call is superfluous,
everything else should work fine.

Chris Angelico
 
T

Tim Chase

I need a dictionary that returns the same value for multiple keys.

i.e.

(1)>>> dict = { (1,2,3) : 'text' , (5,6,7) : 'other text' }
(2)>>>dict[1]
(3) 'text'

I cant figure out what i need on line 2 to make this scenario work. Is
there a simple way to check if the a number is present in the key and
then return the value?

You could use something like

d = dict(
(k,v)
for keys in (
((1,2,3), 'text'),
((5,6,7), 'other text'),
)
for k in keys
)

which seems to do what you want with minimal redundancy of
declarations.

-
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top