Re: [Tutor] changing dictionary to lowercase

2011-10-28 Thread Albert-Jan Roskam
It would be nice to generalize the solution so it could also handle
definitions={Deprecated: No longer in use, DEPRECATED:  No longer in 
use}
These are unique now, but after turning them into lower case not anymore. 
new_d = {}
for d in definitions:
    try:
    new_d[d.lower()].append(definitions[d])
    except TypeError:
    new_d[d.lower()] = [definitions[d]]
 

Cheers!!
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the Romans ever done for us?
~~



From: Joel Goldstick joel.goldst...@gmail.com
To: tutor@python.org
Sent: Thursday, October 27, 2011 8:36 PM
Subject: Re: [Tutor] changing dictionary to lowercase





On Thu, Oct 27, 2011 at 2:25 PM, ADRIAN KELLY kellyadr...@hotmail.com wrote:


Hi all,
is it possible to change a dictionary list to lowercase..without having to 
retype?
e.g. definitions={Deprecated: No longer in use, Depreciation: fall in 
value of an asset}
 
i have tried definitions=definitions.lower()
 
regards
adrian


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

There is a string method called lower so 'Bob'.lower() will return 'bob'

You can't alter the keys in a dictionary because they are immutable -- they 
can't be changed

But you can loop through your dictionary, make new keys lowercase and copy the 
values associated with each key

like this:

 new_d = {}

 for d in definitions:
...   new_d[d.lower()] = definitions[d]
... 
 new_d
{'deprecated': 'No longer in use', 'depreciation': 'fall in value of an asset'}
 




-- 
Joel Goldstick


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing dictionary to lowercase

2011-10-28 Thread Christian Witts

On 2011/10/28 11:51 AM, Albert-Jan Roskam wrote:

It would be nice to generalize the solution so it could also handle
definitions={Deprecated: No longer in use, DEPRECATED:  No 
longer in use}

These are unique now, but after turning them into lower case not anymore.
new_d = {}
for d in definitions:
try:
new_d[d.lower()].append(definitions[d])
except TypeError:
new_d[d.lower()] = [definitions[d]]
Cheers!!
Albert-Jan


snip


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
To save yourself the try/except you can use defaultdict which is part of 
the collections module.


from collections import defaultdict
new_d = defaultdict(list)
for key, value in definitions.iteritems():
new_d[key.lower()].append(value)

--

Christian Witts
Python Developer

//
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing dictionary to lowercase

2011-10-28 Thread bob gailer

Always reply-all so a copy goes to the tutor list.

Always put your responses following the question rather than at the top 
of the email.


On 10/28/2011 8:28 AM, Adrian wrote:

Thats the original alright bob, id like to change keys to lowercase
Thanks
Adrian

Sent from my iPad

On 27 Oct 2011, at 22:49, bob gailer bgai...@gmail.com 
mailto:bgai...@gmail.com wrote:



On 10/27/2011 2:25 PM, ADRIAN KELLY wrote:


Hi all,
is it possible to change a dictionary list to lowercase..without 
having to retype?
e.g. definitions={Deprecated: No longer in use, Depreciation: 
fall in value of an asset}


There seems to be some confusion both in the question and the 
proposed solutions regarding lowercase.


Re your e.g. - is that the original or the result?

It's best to show both.

I have to assume that your e.g. is the original since it contains 
upper case letters.


Do you want to change the case of the keys, values or both?

--
Bob Gailer
919-636-4239
Chapel Hill NC



--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing dictionary to lowercase

2011-10-28 Thread Adrian
Ok boss point noted


Sent from my iPad

On 28 Oct 2011, at 19:05, bob gailer bgai...@gmail.com wrote:

 Always reply-all so a copy goes to the tutor list.
 
 Always put your responses following the question rather than at the top of 
 the email.
 
 On 10/28/2011 8:28 AM, Adrian wrote:
 
 Thats the original alright bob, id like to change keys to lowercase
 Thanks
 Adrian
 
 Sent from my iPad
 
 On 27 Oct 2011, at 22:49, bob gailer bgai...@gmail.com wrote:
 
 On 10/27/2011 2:25 PM, ADRIAN KELLY wrote:
 
 
 Hi all,
 is it possible to change a dictionary list to lowercase..without having to 
 retype?
 e.g. definitions={Deprecated: No longer in use, Depreciation: fall 
 in value of an asset}
 
 There seems to be some confusion both in the question and the proposed 
 solutions regarding lowercase.
 
 Re your e.g. - is that the original or the result? 
 
 It's best to show both. 
 
 I have to assume that your e.g. is the original since it contains upper 
 case letters. 
 
 Do you want to change the case of the keys, values or both? 
 
 -- 
 Bob Gailer
 919-636-4239
 Chapel Hill NC
 
 
 
 -- 
 Bob Gailer
 919-636-4239
 Chapel Hill NC
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] changing dictionary to lowercase

2011-10-27 Thread ADRIAN KELLY


Hi all,
is it possible to change a dictionary list to lowercase..without having to 
retype?
e.g. definitions={Deprecated: No longer in use, Depreciation: fall in 
value of an asset}
 
i have tried definitions=definitions.lower()
 
regards
adrian
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing dictionary to lowercase

2011-10-27 Thread Steve Willoughby

On 27-Oct-11 11:25, ADRIAN KELLY wrote:


Hi all,
is it possible to change a dictionary list to lowercase..without having
to retype?
e.g. definitions={Deprecated: No longer in use, Depreciation:
fall in value of an asset}

i have tried definitions=definitions.lower()


lower() is not a dictionary method, it's a string method (i.e., 
dictionaries have no idea how to lowercase themselves, but strings do).


So what you need to do is iterate over the list of dictionary members 
and re-create a new dictionary with lowercased versions of the strings 
(that's easier than changing the dictionary in-place, especially if 
you're lowercasing the keys, since keys are immutable--you'd have to 
delete the old one and re-store the data under the lowercased key anyway)


There are several ways to do that, including loops and list comprehensions.

Does that nudge you in the right direction?

If you're still stuck, let us know.
--steve

--
Steve Willoughby / st...@alchemy.com
A ship in harbor is safe, but that is not what ships are built for.
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing dictionary to lowercase

2011-10-27 Thread Joel Goldstick
On Thu, Oct 27, 2011 at 2:25 PM, ADRIAN KELLY kellyadr...@hotmail.comwrote:


 Hi all,
 is it possible to change a dictionary list to lowercase..without having to
 retype?
 e.g. definitions={Deprecated: No longer in use, Depreciation: fall
 in value of an asset}

 i have tried definitions=definitions.lower()

 regards
 adrian


 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

 There is a string method called lower so 'Bob'.lower() will return 'bob'

You can't alter the keys in a dictionary because they are immutable -- they
can't be changed

But you can loop through your dictionary, make new keys lowercase and copy
the values associated with each key

like this:

 new_d = {}

 for d in definitions:
...   new_d[d.lower()] = definitions[d]
...
 new_d
{'deprecated': 'No longer in use', 'depreciation': 'fall in value of an
asset'}





-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing dictionary to lowercase

2011-10-27 Thread bob gailer

On 10/27/2011 2:25 PM, ADRIAN KELLY wrote:


Hi all,
is it possible to change a dictionary list to lowercase..without 
having to retype?
e.g. definitions={Deprecated: No longer in use, Depreciation: 
fall in value of an asset}


There seems to be some confusion both in the question and the proposed 
solutions regarding lowercase.


Re your e.g. - is that the original or the result?

It's best to show both.

I have to assume that your e.g. is the original since it contains upper 
case letters.


Do you want to change the case of the keys, values or both?

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing dictionary to lowercase

2011-10-27 Thread Alan Gauld

On 27/10/11 19:25, ADRIAN KELLY wrote:


is it possible to change a dictionary list to lowercase..without having
to retype?
e.g. definitions={Deprecated: No longer in use, Depreciation:
fall in value of an asset}


You've posted a few similar type questions lately that look suspiciously 
like homework exercises. If that is the case we are still happy to help 
but we try to avoid giving you the answer directly.

And we do like to see what you have tried first.


i have tried definitions=definitions.lower()


Thats a start but as you found out doesn't even execute.

Others have suggested some options but it would help us if you tell us 
which version of Python you are using because Python 3 offers some 
different mechanisms to Python v2. (You can tell us the OS too for 
completeness but it probably doesn't make much difference for the 
questions you have been asking)


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor