niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
dict indexed by lists - ?
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
Alexander Zatvornitskiy
*nix forums beginner


Joined: 03 Mar 2005
Posts: 20

PostPosted: Thu Feb 03, 2005 11:58 pm    Post subject: dict indexed by lists - ? Reply with quote

Привет All!

04 февраля 2005 в 02:47, Alexander Zatvornitskiy в своем письме к All писал:
AZ> I'am trying to make something like this:
AZ> CPT={ ['b0','c0']:1, ['b0','c1']:0, ['b1','c0']:3, ['b1','c1']:1 }

Thanks, I fix it - i use lists.

AZ> and, later, modifying it like this:
AZ> key[2]=up.next_state()

replaced with:
k=k[:cur_idx-1]+up.next_state()+k[cur_id+1:]

AZ> ...
AZ> print CPT[key]

AZ> Any suggestions?


AZ> Alexander, zatv@bk.ru


Alexander, zatv@bk.ru
Back to top
Steven Bethard
*nix forums Guru


Joined: 20 Feb 2005
Posts: 773

PostPosted: Thu Feb 03, 2005 11:42 pm    Post subject: Re: dict indexed by lists - ? Reply with quote

Alexander Zatvornitskiy wrote:
Quote:
Hello All!

I'am trying to make something like this:

CPT={ ['b0','c0']:1, ['b0','c1']:0, ['b1','c0']:3, ['b1','c1']:1 }

but python says "TypeError: list objects are unhashable"

I can replace list with touple: CPT={ ('b0','c0'):1, ('b0','c1'):0, ...and so
on. But, where is one problem: indexes (or, more precisely, keys) are generated
dynamically, like code below! I can't do it with touples.


key=[]
for up in uplinks:
key.append(up.state(0))


and, later, modifying it like this:
key[2]=up.next_state()
...
print CPT[key]

Any suggestions?

You can manually add the tuple calls:

CPT = {('b0','c0'):1, ('b0','c1'):0, ('b1','c0'):3, ('b1','c1'):1}
...
key = [up.state(0) for up in uplinks]
...
key[2] = up.next_state()
...
print CPT[tuple(key)]

I've only added the tuple call when you actually access CPT, but
depending on your code, you might find that you can make the tuple call
earlier. For example, the following code should work:

CPT={('b0','c0'):1, ('b0','c1'):0, ('b1','c0'):3, ('b1','c1'):1}
...
key = tuple([up.state(0) for up in uplinks])
...
print CPT[key]

where basically you convert key back to a tuple any time it's changed.

STeVe
Back to top
Alexander Zatvornitskiy
*nix forums beginner


Joined: 03 Mar 2005
Posts: 20

PostPosted: Thu Feb 03, 2005 10:47 pm    Post subject: dict indexed by lists - ? Reply with quote

Hello All!

I'am trying to make something like this:

CPT={ ['b0','c0']:1, ['b0','c1']:0, ['b1','c0']:3, ['b1','c1']:1 }

but python says "TypeError: list objects are unhashable"

I can replace list with touple: CPT={ ('b0','c0'):1, ('b0','c1'):0, ...and so
on. But, where is one problem: indexes (or, more precisely, keys) are generated
dynamically, like code below! I can't do it with touples.


key=[]
for up in uplinks:
key.append(up.state(0))


and, later, modifying it like this:
key[2]=up.next_state()
...
print CPT[key]

Any suggestions?


Alexander, zatv@bk.ru
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 11:19 pm | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts FAQ 2.13 What mailing lists are there for Perl? PerlFAQ Server Perl 0 Mon Jul 17, 2006 1:03 pm
No new posts Mailing lists again Joshua D. Drake PostgreSQL 1 Wed Jul 12, 2006 8:17 pm
No new posts Perl +lists/unions/intersection: oleg Perl 9 Wed Jul 12, 2006 2:13 am
No new posts FAQ 4.46 How do I handle linked lists? PerlFAQ Server Perl 0 Fri Jul 07, 2006 7:03 pm
No new posts how can I avoid abusing lists? Thomas Nelson python 16 Fri Jul 07, 2006 4:18 pm

Fish Tank Help | Capital One Credit Cards | Web Advertising | Bankruptcy | Vietnamese Magazine
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1846s ][ Queries: 20 (0.1095s) ][ GZIP on - Debug on ]