473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Dict- Arranging a elements in a order

440 256MB
Hi,

In case of the 'dict',the stored elements will not be in a order.It will be randomly arranged ( not as list,in list the elements will be arranged in order as we store it).
For Example :
---------------------
Input : Elements stored in order
>>> d = {1:[1,1],100:[1,2],2:[1]}

output ( Not as expected): Order of the dict Elements as we stored has been changed
>>> d
{1: [1, 1], 2: [1], 100: [1, 2]}
>>>

How to arrange the elements in a order inside a dict?

Thanks
PSB
Jul 24 '07 #1
7 2506
bartonc
6,596 Expert 4TB
Hi,

In case of the 'dict',the stored elements will not be in a order.It will be randomly arranged ( not as list,in list the elements will be arranged in order as we store it).
For Example :
---------------------
Input : Elements stored in order
>>> d = {1:[1,1],100:[1,2],2:[1]}

output ( Not as expected): Order of the dict Elements as we stored has been changed
>>> d
{1: [1, 1], 2: [1], 100: [1, 2]}
>>>

How to arrange the elements in a order inside a dict?

Thanks
PSB
Use a tuple instead if a ditionary...
Expand|Select|Wrap|Line Numbers
  1. >>> t = ({1:(1,1)},{100:(1,2)},{2:(1,)})
  2. >>> t
  3. ({1: (1, 1)}, {100: (1, 2)}, {2: (1,)})
  4. >>> t[1]
  5. {100: (1, 2)}
  6. >>> 
I have used tuples as the items of the dictionaries as an efficiency boost. Lists take much longer to create in memory that do tuples.
Jul 24 '07 #2
bartonc
6,596 Expert 4TB
Use a tuple instead if a ditionary...
Expand|Select|Wrap|Line Numbers
  1. >>> t = ({1:(1,1)},{100:(1,2)},{2:(1,)})
  2. >>> t
  3. ({1: (1, 1)}, {100: (1, 2)}, {2: (1,)})
  4. >>> t[1]
  5. {100: (1, 2)}
  6. >>> 
I have used tuples as the items of the dictionaries as an efficiency boost. Lists take much longer to create in memory that do tuples.
For example, this implementation is almost four times faster than yours:
Expand|Select|Wrap|Line Numbers
  1. >>> import timeit
  2.  
  3. >>> # a million dictionaries of int:tuple pairs
  4. >>> t = timeit.Timer("d = {1:(1,1),100:(1,2),2:(1,)}")
  5. >>> t.timeit()
  6. 1.1584151058304997
  7.  
  8. >>> # a million tuples with 3 dictionaries of int:tuple pairs
  9. >>> t = timeit.Timer("t = ({1:(1,1)},{100:(1,2)},{2:(1,)})")
  10. >>> t.timeit()
  11. 1.942572589533027
  12.  
  13. >>> # a million dictionaries of int:list pairs
  14. >>> t = timeit.Timer("d = {1:[1,1],100:[1,2],2:[1]}")
  15. >>> t.timeit()
  16. 4.0557003753270351
  17. >>> 
And creating three dictionaries with lists as the items took agonizingly long:
Expand|Select|Wrap|Line Numbers
  1. >>> # a million tuples with 3 dictionaries of int:list pairs
  2. >>> t = timeit.Timer("t = ({1:[1,1]},{100:[1,2]},{2:[1]})")
  3. >>> t.timeit()
  4. 4.8632546873974434
  5. >>> 
Jul 24 '07 #3
bvdet
2,851 Expert Mod 2GB
Hi,

In case of the 'dict',the stored elements will not be in a order.It will be randomly arranged ( not as list,in list the elements will be arranged in order as we store it).
For Example :
---------------------
Input : Elements stored in order
>>> d = {1:[1,1],100:[1,2],2:[1]}

output ( Not as expected): Order of the dict Elements as we stored has been changed
>>> d
{1: [1, 1], 2: [1], 100: [1, 2]}
>>>

How to arrange the elements in a order inside a dict?

Thanks
PSB
There are several implementations of ordered dictionaries <url remove> on activestate
Jul 24 '07 #4
bartonc
6,596 Expert 4TB
There are several implementations of ordered dictionaries <url remove> on activestate
Sorry BV. The powers that be would have my head if I let a link to a competitive site stay. Besides, I'm sure our friend, here, wants to maintain the order of creation; not the key value order (which of course can't be done in a dictionary - for good reason).
Jul 24 '07 #5
bvdet
2,851 Expert Mod 2GB
Sorry BV. The powers that be would have my head if I let a link to a competitive site stay. Besides, I'm sure our friend, here, wants to maintain the order of creation; not the key value order (which of course can't be done in a dictionary - for good reason).
Oops! I did not consider that site as competition. A Google search on "ordered dictionary python" provides plenty of links. The implementations I looked at preserved the order of creation.
Jul 24 '07 #6
bartonc
6,596 Expert 4TB
Oops! I did not consider that site as competition. A Google search on "ordered dictionary python" provides plenty of links. The implementations I looked at preserved the order of creation.
I've held back linking there due to their forum structure.

Our friend here has complained that his python implementation is 4 to 5 times slower that another implementation. Simple is probably better than anything else (with an eye toward efficiency). <Just my impressions on the situation.>
Jul 24 '07 #7
bartonc
6,596 Expert 4TB
Oops! I did not consider that site as competition. A Google search on "ordered dictionary python" provides plenty of links. The implementations I looked at preserved the order of creation.
Here is an example of what is allowed from such sites:
Expand|Select|Wrap|Line Numbers
  1. extract = lambda keys, dict: reduce(lambda x, y: x.update({y[0]:y[1]}) or x,
  2.                                     map(None, keys, map(dict.get, keys)), {})
  3.  
That way they get the credit that is due, the member gets the info with a bonus, an we have not provided a link.
Jul 26 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after...
2
by: GrelEns | last post by:
hello, i would like if this behaviour can be obtained from python : trap an attributeError from inside a subclassing dict class... (here is a silly examples to explain my question) class...
2
by: Alex | last post by:
Entering >>> help(dict) Help on class dict in module __builtin__: class dict(object) | dict() -> new empty dictionary. | dict(mapping) -> new dictionary initialized from a mapping object's...
3
by: Bengt Richter | last post by:
Has anyone found a way besides not deriving from dict? Shouldn't there be a way? TIA (need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-) I guess I can just document...
15
by: Cruella DeVille | last post by:
I'm trying to implement a bookmark-url program, which accepts user input and puts the strings in a dictionary. Somehow I'm not able to iterate myDictionary of type Dict{} When I write print...
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
3
by: james_027 | last post by:
hi, a_dict = {'name':'apple', 'color':'red', 'texture':'smooth', 'shape':'sphere'} is there any difference between .. for key in a_dict: from
8
by: rodrigo | last post by:
Im using this construct a lot: if dict.has_key(whatever): dict += delta else: dict = 1 sometimes even nested: if dict.has_key(whatever):
20
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with 5 keys. dict = {1: , 2: , 900000: , 900001:...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.