473,799 Members | 3,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ordered dictionary?

I need to associate a string (key) with an integer (value). A dictionary
would do the job, except for the fact that it must be ordered. I also
need to look up the value for a specific key easily, so a list of tuples
wouldn't work.

Jul 18 '05 #1
14 3190
Leif K-Brooks wrote:
I need to associate a string (key) with an integer (value). A dictionary
would do the job, except for the fact that it must be ordered. I also
need to look up the value for a specific key easily, so a list of tuples
wouldn't work.


A dictionary could be used. When you need to managed the items in order,
what you would do is called dict.keys(), followed by a sort.

I don't know if this helps at all...

--
Glitch

http://andres980.tripod.com/

"That is the law of the jungle. The hunters and the hunted. Scrap or
be scrapped!"
"Animals hunt to SURVIVE!"
"And what do you think war is about?!"
-- Dinobot and Tigatron, "The Law of the Jungle"
Jul 18 '05 #2
Leif K-Brooks wrote:
I need to associate a string (key) with an integer (value). A dictionary
would do the job, except for the fact that it must be ordered. I also
need to look up the value for a specific key easily, so a list of tuples
wouldn't work.


How must the dictionary be ordered? Do you need the keys or the values
sorted?

- Josiah
Jul 18 '05 #3
>>>>> "Leif" == Leif K-Brooks <eu*****@ecritt ers.biz> writes:

Leif> I need to associate a string (key) with an integer
Leif> (value). A dictionary would do the job, except for the fact
Leif> that it must be ordered. I also need to look up the value
Leif> for a specific key easily, so a list of tuples wouldn't
Leif> work.

google : python ordered dictionary
click : I'm feeling lucky

JDH

Jul 18 '05 #4
# I need to associate a string (key) with an integer (value). A dictionary
# would do the job, except for the fact that it must be ordered. I also
# need to look up the value for a specific key easily, so a list of tuples
# wouldn't work.

As the other posts on this thread suggest, dictionaries -- by virtue
of their structure -- have no inherently meaningful ordering. You'll
have to either sort the keys (the easy way) or design a wrapper for
the dictionary that maintains a sorted list of the dictionary's
keys and updates it as necessary (the hard way).

--

Jonathan Daugherty
http://www.cprogrammer.org

"It's a book about a Spanish guy called Manual, you should read it."
-- Dilbert

Jul 18 '05 #5
"Leif K-Brooks" <eu*****@ecritt ers.biz> wrote in message
news:le******** **********@news hog.newsread.co m...
I need to associate a string (key) with an integer (value). A dictionary
would do the job, except for the fact that it must be ordered. I also
need to look up the value for a specific key easily, so a list of tuples
wouldn't work.

If you really need to access the dictionary in sorted key order, is this so
difficult?

dKeys = d.keys()
dKeys.sort()
for k in dKeys:
# do stuff with k and d[k], such as:
print k, "=", d[k]

Or if you are worried about updates to d between the time of key retrieval
and time of traversal (for instance, if a separate thread were to delete one
of the keyed entries), take a snapshot as a list:

dItems = d.items() # from here on, you are insulated from changes to
dictionary 'd'
dItems.sort() # implicitly sorts by key
dItems.sort( lambda a,b: a[1]-b[1] ) # sorts by value, if you so prefer
for k,v in dItems:
# do stuff with k and v, such as:
print k, "=", v # <-- added benefit here of not re-accessing
the list by key

-- Paul
Jul 18 '05 #6
Josiah Carlson wrote:
I need to associate a string (key) with an integer (value). A
dictionary would do the job, except for the fact that it must be
ordered. I also need to look up the value for a specific key easily,
so a list of tuples wouldn't work.

How must the dictionary be ordered? Do you need the keys or the values
sorted?


Sorry for not making that clear, I need it sorted by the order of
insertion. Looks like
http://aspn.activestate.com/ASPN/Coo.../Recipe/107747 might do
what I want...

Jul 18 '05 #7
Leif K-Brooks wrote:
Josiah Carlson wrote:
I need to associate a string (key) with an integer (value). A
dictionary would do the job, except for the fact that it must be
ordered. I also need to look up the value for a specific key easily,
so a list of tuples wouldn't work.


How must the dictionary be ordered? Do you need the keys or the
values sorted?

Sorry for not making that clear, I need it sorted by the order of
insertion. Looks like
http://aspn.activestate.com/ASPN/Coo.../Recipe/107747 might do
what I want...


That should do. Looks promising, but I think it uses the old style of
extending the dictionary (Not that it's bad) ...

--
Glitch

http://andres980.tripod.com/

One good shot is worth a hundred bad ones!
-- Big Shot (G1)
Jul 18 '05 #8
Op 2004-01-22, Paul McGuire schreef <pt***@users.so urceforge.net>:
"Leif K-Brooks" <eu*****@ecritt ers.biz> wrote in message
news:le******** **********@news hog.newsread.co m...
I need to associate a string (key) with an integer (value). A dictionary
would do the job, except for the fact that it must be ordered. I also
need to look up the value for a specific key easily, so a list of tuples
wouldn't work.

If you really need to access the dictionary in sorted key order, is this so
difficult?

dKeys = d.keys()
dKeys.sort()
for k in dKeys:
# do stuff with k and d[k], such as:
print k, "=", d[k]

Or if you are worried about updates to d between the time of key retrieval
and time of traversal (for instance, if a separate thread were to delete one
of the keyed entries), take a snapshot as a list:

dItems = d.items() # from here on, you are insulated from changes to
dictionary 'd'
dItems.sort() # implicitly sorts by key
dItems.sort( lambda a,b: a[1]-b[1] ) # sorts by value, if you so prefer
for k,v in dItems:
# do stuff with k and v, such as:
print k, "=", v # <-- added benefit here of not re-accessing
the list by key


Well I too sometimes need the keys in a dictionary to be sorted and your
solutions wouldn't help. The problem is the following.

I have a number of key value pairs, like names and telephone numbers.
Just more subject to change. Now I want the telephone numbers of everyone
whose name starts with "jan".

Or I just inserted a name and want to know who is alphabetically next.
Or I want to know who is first or last.

--
Antoon Pardon
Jul 18 '05 #9
Hi

Maybe this code will help:

import operator

def filterList(p_li st, p_value):
l_len = len(p_list)
l_temp = map(None, (p_value,)*l_le n, p_list)
l_temp = filter(lambda x: x[1].find(x[0])==0, l_temp)
return map(operator.ge titem, l_temp, (-1,)*len(l_temp) )

phones_dict = {'jansen' : '0000', 'xxx' : '1111', 'jan2' : '2222'}
names_list = filterList(phon es_dict.keys(), 'jan')
phones_list = map(phones_dict .get, names_list)

print phones_list

This extracts from the dictionary the telephone(value s) numbers for
names(keys) starting with 'jan'...

Dragos
Op 2004-01-22, Paul McGuire schreef <pt***@users.so urceforge.net>:
"Leif K-Brooks" <eu*****@ecritt ers.biz> wrote in message
news:le******** **********@news hog.newsread.co m...
I need to associate a string (key) with an integer (value). A dictionary would do the job, except for the fact that it must be ordered. I also
need to look up the value for a specific key easily, so a list of tuples wouldn't work.

If you really need to access the dictionary in sorted key order, is this so difficult?

dKeys = d.keys()
dKeys.sort()
for k in dKeys:
# do stuff with k and d[k], such as:
print k, "=", d[k]

Or if you are worried about updates to d between the time of key retrieval and time of traversal (for instance, if a separate thread were to delete one of the keyed entries), take a snapshot as a list:

dItems = d.items() # from here on, you are insulated from changes to dictionary 'd'
dItems.sort() # implicitly sorts by key
dItems.sort( lambda a,b: a[1]-b[1] ) # sorts by value, if you so prefer for k,v in dItems:
# do stuff with k and v, such as:
print k, "=", v # <-- added benefit here of not re-accessing the list by key


Well I too sometimes need the keys in a dictionary to be sorted and your
solutions wouldn't help. The problem is the following.

I have a number of key value pairs, like names and telephone numbers.
Just more subject to change. Now I want the telephone numbers of everyone
whose name starts with "jan".

Or I just inserted a name and want to know who is alphabetically next.
Or I want to know who is first or last.

--
Antoon Pardon
--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

210
10566
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again I am missing that feature. Maybe there is something wrong with my programming style, but I rather think it is generally useful. I fully agree with the following posting where somebody complains why so very basic and useful things are not part...
8
1676
by: Fuzzyman | last post by:
Sorry for this hurried message - I've done a new implementation of out ordered dict. This comes out of the discussion on this newsgroup (see blog entry for link to archive of discussion). See the latest blog entry to get at it : http://www.voidspace.org.uk/python/weblog/index.shtml Criticism solicited (honestly) :-) We (Nicola Larosa and I) haven't yet made any optimisations - but there
22
2378
by: bearophileHUGS | last post by:
>From this interesting blog entry by Lawrence Oluyede: http://www.oluyede.org/blog/2006/07/05/europython-day-2/ and the Py3.0 PEPs, I think the people working on Py3.0 are doing a good job, I am not expert enough (so I don't post this on the Py3.0 mailing list), but I agree with most of the things they are agreeing to. Few notes: - input() vanishes and raw_input() becomes sys.stdin.readline(). I think a child that is learning to program...
1
1498
by: Fuzzyman | last post by:
After a break of almost a year there has been an update to `odict the Ordered Dictionary <http://www.voidspace.org.uk/python/odict.html>`_. The latest version is 0.2.2, with changes implemented by Nicola Larosa. Despite over 700 downloads since May (plus 1300 as part of `pythonutils <http://www.voidspace.org.uk/python/pythonutils.html>`_) there have been no bug reports, only improvements _. {sm;:-)} * `Quick Download
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5463
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.