|
|
|
|
|
|
| Author |
Message |
Alexander Zatvornitskiy *nix forums beginner
Joined: 03 Mar 2005
Posts: 20
|
Posted: Thu Feb 03, 2005 11:58 pm Post subject:
dict indexed by lists - ?
|
|
|
Привет 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
|
Posted: Thu Feb 03, 2005 11:42 pm Post subject:
Re: dict indexed by lists - ?
|
|
|
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
|
Posted: Thu Feb 03, 2005 10:47 pm Post subject:
dict indexed by lists - ?
|
|
|
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 |
|
 |
|
|
The time now is Thu Jan 08, 2009 11:19 pm | All times are GMT
|
|
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
|
|