Hi all,
We know that list cannot be used as key of dictionary. So, how to work
around it?
For example, there is random list like l=[1,323,54,67].
Any suggestions are welcome!
Best regards,
Davy 16 28703
Davy wrote:
Hi all,
We know that list cannot be used as key of dictionary.
Yeah, but do we know why ?
So, how to work
around it?
That's a subsidiary question.
>
For example, there is random list like l=[1,323,54,67].
Don't use 1owercase L as a variab1e name, p1ease !
>
Any suggestions are welcome!
>>{tuple([1,323,54,67]):666}
{(1, 323, 54, 67): 666}
On Nov 5, 10:53 pm, Davy <zhushe...@gmai l.comwrote:
Hi all,
We know that list cannot be used as key of dictionary. So, how to work
around it?
For example, there is random list like l=[1,323,54,67].
Any suggestions are welcome!
Best regards,
Davy
Use a tuple instead.
>>d = {} d[tuple([1,2,3,4])] = 'hello world' d
{(1, 2, 3, 4): 'hello world'}
>>d[1,2,3,4]
'hello world'
Matt
Hi Matimus and Boris,
Thank you :)
And a further question about vector above rank 1, how can I use it as
the key of dictionary?
For example, if I have list like L=[[1,2,3],[4,5,6,7]],
Then I do L_tuple = tuple(L)
>>L_tuple = ([1,2,3],[4,5,6,7])
But {L_tuple:'hello '} cause an error?
Best regards,
Davy
On Nov 6, 3:09 pm, Matimus <mccre...@gmail .comwrote:
On Nov 5, 10:53 pm, Davy <zhushe...@gmai l.comwrote:
Hi all,
We know that list cannot be used as key of dictionary. So, how to work
around it?
For example, there is random list like l=[1,323,54,67].
Any suggestions are welcome!
Best regards,
Davy
Use a tuple instead.
>d = {} d[tuple([1,2,3,4])] = 'hello world' d
{(1, 2, 3, 4): 'hello world'}>>d[1,2,3,4]
'hello world'
Matt
Davy wrote:
Hi Matimus and Boris,
Thank you :)
And a further question about vector above rank 1, how can I use it as
the key of dictionary?
For example, if I have list like L=[[1,2,3],[4,5,6,7]],
Then I do L_tuple = tuple(L)
>>>L_tuple = ([1,2,3],[4,5,6,7])
But {L_tuple:'hello '} cause an error?
Yes, because your key still contains mutable elements. That should not
surprise you. If it does, please (re-)read
<URL:http://docs.python.org/tut/node7.html#SECT ION007500000000 000000000>
and <URL:http://docs.python.org/lib/typesmapping.ht ml>.
maybe something like this could help:
def tupleize(non_tu ple):
try:
return tuple(tupleize( thing) for thing in non_tuple)
except TypeError:
# non_tuple is not iterable
return non_tuple
/W
Wildemar Wildenburger <la*********@kl apptsowieso.net wrote:
maybe something like this could help:
def tupleize(non_tu ple):
try:
return tuple(tupleize( thing) for thing in non_tuple)
except TypeError:
# non_tuple is not iterable
return non_tuple
Just don't try passing that a string or anything containing a string.
On Nov 6, 3:58 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
Wildemar Wildenburger <lasses_w...@kl apptsowieso.net wrote:
maybe something like this could help:
def tupleize(non_tu ple):
try:
return tuple(tupleize( thing) for thing in non_tuple)
except TypeError:
# non_tuple is not iterable
return non_tuple
Just don't try passing that a string or anything containing a string.
Untested
def tupleize(non_tu ple):
if isinstance(non_ tuple, str):
return non_tuple
try:
return tuple(tupleize( thing) for thing in non_tuple)
except TypeError:
# non_tuple is not iterable
return non_tuple
On Tue, 6 Nov 2007, Boris Borcic wrote:
>We know that list cannot be used as key of dictionary.
Yeah, but do we know why ?
I think, because lists are mutable and a key of a dictionary
MUST be unmutable, not to crash the dictionary by accidently
changing one of its keys!
Mike
And there may be more complex list(vector like 3 or 4 dimentional data
structure), is there any easy method to tackle this problem?
Any suggestions are welcome!
Best regards,
Davy
On Nov 6, 4:50 pm, Davy <zhushe...@gmai l.comwrote:
Hi Matimus and Boris,
Thank you :)
And a further question about vector above rank 1, how can I use it as
the key of dictionary?
For example, if I have list like L=[[1,2,3],[4,5,6,7]],
Then I do L_tuple = tuple(L)>>L_tup le = ([1,2,3],[4,5,6,7])
But {L_tuple:'hello '} cause an error?
Best regards,
Davy
On Nov 6, 3:09 pm, Matimus <mccre...@gmail .comwrote:
On Nov 5, 10:53 pm, Davy <zhushe...@gmai l.comwrote:
Hi all,
We know that list cannot be used as key of dictionary. So, how to work
around it?
For example, there is random list like l=[1,323,54,67].
Any suggestions are welcome!
Best regards,
Davy
Use a tuple instead.
>>d = {}
>>d[tuple([1,2,3,4])] = 'hello world'
>>d
{(1, 2, 3, 4): 'hello world'}>>d[1,2,3,4]
'hello world'
Matt- Hide quoted text -
- Show quoted text -
On Nov 6, 4:08 am, Dustan <DustanGro...@g mail.comwrote:
On Nov 6, 3:58 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
Wildemar Wildenburger <lasses_w...@kl apptsowieso.net wrote:
maybe something like this could help:
def tupleize(non_tu ple):
try:
return tuple(tupleize( thing) for thing in non_tuple)
except TypeError:
# non_tuple is not iterable
return non_tuple
Just don't try passing that a string or anything containing a string.
Untested
def tupleize(non_tu ple):
if isinstance(non_ tuple, str):
return non_tuple
try:
return tuple(tupleize( thing) for thing in non_tuple)
except TypeError:
# non_tuple is not iterable
return non_tuple
isinstance(x,ba sestring)
is preferred over
isinstance(x,st r)
in case x is a unicode.
-- Paul This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Julien Sagnard |
last post by:
Hi all,
When passing a list as default argument for a function, only one list is
created for all calls.
exemple:
>>> def f(a=):
.... a.append("x")
.... print a
....
>>> f()
|
by: flamesrock |
last post by:
Hi,
Basically, what I'm trying to do is store large amounts of data in a
list or dictionary and then convert that to a custom formatted xml
file.
My list looks roughly like this:
(d,r,p]]])
My question is, would it be faster to use a dictionary if the elements
|
by: Carl J. Van Arsdall |
last post by:
Python Gurus:
Let me elaborate a bit more on this question. Basically, I want to know
if there is some data structure in python that maps a string function
name to an address of a function or something to that nature.
If this is confusing, let me describe what I want to do and see if
anyone has any ideas.
basically we have:
|
by: Stef Mientki |
last post by:
If I'm not mistaken, I read somewhere that you can use
function-names/references in lists and/or dictionaries, but now I can't
find it anymore.
The idea is to build a simulator for some kind of micro controller (just
as a general practise, I expect it too be very slow ;-).
opcodes ={
1: ('MOV', function1, ...),
2: ('ADD', function2, ),
|
by: psbasha |
last post by:
Hi,
Is it mandatory to clear the data from the List ,Dictionary and Tuples after using it?.
- PSB
| |
by: mk |
last post by:
Calvin Spealman wrote:
Well, basically nothing except I need to remember I have to do that.
Suppose one does that frequently in a program. It becomes tedious. I
think I will define some helper function then:
.... fundict = newfun
....
|
by: Manoj |
last post by:
Hello All,
I am very new to python. Any help will be highly appreciated. Thanks
I have a list of dictionaries:
a =
I would like to :
|
by: dineshv |
last post by:
I want to see if there is an alternative method for fast list
traversal. The code is very simple:
dict_long_lists = defaultdict(list)
for long_list in dict_long_lists.itervalues()
for element in long_list:
array_a = m + n + p # m,n,p
are variable numbers
The long_list's are read from a defaultdict(list) dictionary and so
|
by: =?Utf-8?B?anAybXNmdA==?= |
last post by:
I'm pulling records from or database by dates, and there are multiple records
for each part number.
The part numbers are being stored as strings in a List in a custom Employee
class:
List<stringParts = new List<string>();
If a part number is not already in the list, a new entry is created for an
employee that gets credited this part...
|
by: sachin2 |
last post by:
I am using 3 types of dictionaries.
1) Dictionary<string, string > d = new Dictionary<string, string>();
2) Dictionary<string, List<string>> d = new Dictionary<string, List<string>>();
3) Dictionary<string, Dictionary<string, string>> d = new Dictionary<string, Dictionary<string, string>>();
Now I am using GetDictionaryType Function...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |