* FAQ    * Search  * Register * Login 
Active topics
Unanswered topics

All times are UTC-06:00



Post new topic  Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Python: Building a list within the value of a dictionary.
PostPosted: Sun Feb 13, 2011 8:28 am 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
Code:
>>> dic.setdefault("test",[]).append("new")
>>> dic
{'test': ['new']}
>>> dic.setdefault("test",[]).append("blue")
>>> dic
{'test': ['new', 'blue']}
>>> dic.setdefault("test",[]).append("blank")
>>> dic
{'test': ['new', 'blue', 'blank']}
>>> dic.setdefault("test2",[]).append("blank")
>>> dic
{'test': ['new', 'blue', 'blank'], 'test2': ['blank']}
>>> dic.setdefault("test2",[]).append("red")
>>> dic.setdefault("test2",[]).append("white")
>>> dic.setdefault("test3",[]).append("white")
>>> dic
{'test': ['new', 'blue', 'blank'], 'test3': ['white'], 'test2': ['blank', 'red', 'white']}
>>>

I asked for help on the python forum. I had trouble getting the values to update with out replacing each other or not being placed correctly.

I think this solution is really cool!

_________________
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ


Top
   
 Post subject: Re: Python: Building a list within the value of a dictionary
PostPosted: Sun Feb 13, 2011 5:25 pm 
Offline
DBB Master
DBB Master
User avatar

Joined: Sun Sep 05, 1999 2:01 am
Posts: 6377
Location: ☃☃☃
It's funny you mention this. I had the same issue a month or so ago and stumbled upon this solution via Google on the python mailing lists. Like you said, you could implement this pattern without the built in setdefault() method, but the code gets unnecessarily nasty. This probably goes without saying, but then if you want to get with a default without necessarily setting the default, you can just say:
Code:
dict.get('test', [])


Top
   
 Post subject: Re: Python: Building a list within the value of a dictionary
PostPosted: Sun Feb 13, 2011 6:41 pm 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
edit:
will get back to you on this...
Thanks for showing me get()!

_________________
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ


Top
   
 Post subject: Re: Python: Building a list within the value of a dictionary
PostPosted: Fri Feb 18, 2011 10:47 pm 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
Code:
>>> dic.get(str,[]).append("testa")
>>> dic
{}
>>> a=dic.get("as",[])
>>> a
[]
>>> a.append("test")
>>> a
['test']
>>> a=dic.get("as",[])
>>> a
[]


I'm very curious about this. However, I think I need to start my python training over from scratch. There are many fundamentals I think I've skipped over. A user on the python forum I go to offered this: http://openbookproject.net/thinkcs/python/english2e/

Thanks again!

_________________
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ


Top
   
 Post subject: Re: Python: Building a list within the value of a dictionary
PostPosted: Sat Feb 19, 2011 1:44 am 
Offline
DBB Master
DBB Master
User avatar

Joined: Sun Sep 05, 1999 2:01 am
Posts: 6377
Location: ☃☃☃
I doubt you have to relearn it from scratch.

dict.get(key, []) is to getting as dict.setdefault(key, []) is to setting. So get(key, default) just returns the default if nothing is in the dict, but doesn't put anything in it. But setdefault(key, default), before it returns the default, puts it in the dict for that key too. So although in this case they both return a [], only setdefault is returning a reference to a [] already inside the dict.

References are probably something to think more about. Since python has both functional and imperative idioms, it's not always clear which operations mutate lists or which create new ones. For example:
Code:
>>> xs = [1,2,3]
>>> ys = xs
>>> xs.append(4)
>>> xs
[1, 2, 3, 4]
>>> ys
[1, 2, 3, 4]


But
Code:
>>> xs = [1,2,3]
>>> ys = xs
>>> xs = xs + [4]
>>> xs
[1, 2, 3, 4]
>>> ys
[1, 2, 3]


And
Code:
>>> xs
[1, 2, 3, 4]
>>> xs = [1,2,3]
>>> ys = xs[:]
>>> xs.append(4)
>>> xs
[1, 2, 3, 4]
>>> ys
[1, 2, 3]


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 5 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Descent'rs have piloted these pages
 
The layout and contents contained within this site are © DescentBB.net 1997-2006.
Descent, Descent II are © Parallax Software Corporation.
Descent III is Outrage Entertainment.
Descent is a Trademark of Interplay Productions.

Miner Wars™ is trademark of Keen Software House s. r. o.
.


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group