473,411 Members | 2,186 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,411 software developers and data experts.

Accessing dictionary variable data

440 256MB
The following example shows the storing of the data and accessing the data from dictionary variable.The accessing of dictionary variable data is not a orderas what we are storing.

Is my understanding is correct?.Is it possible to retrievethe dictiondary variable data in a order,as what we store?

Thanks in advance
PSB
Expand|Select|Wrap|Line Numbers
  1. >>> dict1= {"E1":[1,2,6,5],"E2":[2,3,7,6],"E3":[3,4,8,7],"E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11]}
  2. >>> print dict1
  3. {'E5': [6, 7, 11, 10], 'E4': [5, 6, 10, 9], 'E6': [7, 8, 12, 11], 'E1': [1, 2, 6, 5], 'E3': [3, 4, 8, 7], 'E2': [2, 3, 7, 6]}
  4. >>> 
Mar 4 '07 #1
6 1972
ghostdog74
511 Expert 256MB
The following example shows the storing of the data and accessing the data from dictionary variable.The accessing of dictionary variable data is not a orderas what we are storing.

Is my understanding is correct?.Is it possible to retrievethe dictiondary variable data in a order,as what we store?

Thanks in advance
PSB
Expand|Select|Wrap|Line Numbers
  1. >>> dict1= {"E1":[1,2,6,5],"E2":[2,3,7,6],"E3":[3,4,8,7],"E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11]}
  2. >>> print dict1
  3. {'E5': [6, 7, 11, 10], 'E4': [5, 6, 10, 9], 'E6': [7, 8, 12, 11], 'E1': [1, 2, 6, 5], 'E3': [3, 4, 8, 7], 'E2': [2, 3, 7, 6]}
  4. >>> 
the typical way to do it is by sorting the keys
Expand|Select|Wrap|Line Numbers
  1. >>> for keys in sorted(dict1.keys()):
  2. ...  print keys, dict1[keys]
  3. ...
  4. E1 [1, 2, 6, 5]
  5. E2 [2, 3, 7, 6]
  6. E3 [3, 4, 8, 7]
  7. E4 [5, 6, 10, 9]
  8. E5 [6, 7, 11, 10]
  9. E6 [7, 8, 12, 11]
  10.  
  11.  
Mar 4 '07 #2
psbasha
440 256MB
In this example the data is available in a order.But if the data is available in this order.

dict1= {E2":[2,3,7,6],"E3":[3,4,8,7],"E1":[1,2,6,5],","E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11],"E3":[3,4,8,7]}

Thanks
PSB
Mar 4 '07 #3
bartonc
6,596 Expert 4TB
In this example the data is available in a order.But if the data is available in this order.

dict1= {E2":[2,3,7,6],"E3":[3,4,8,7],"E1":[1,2,6,5],","E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11],"E3":[3,4,8,7]}

Thanks
PSB
Dictionaries are NOT ordered collections. They are ramdomized for faster retreival (hashing).
Mar 4 '07 #4
ghostdog74
511 Expert 256MB
In this example the data is available in a order.But if the data is available in this order.

dict1= {E2":[2,3,7,6],"E3":[3,4,8,7],"E1":[1,2,6,5],","E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11],"E3":[3,4,8,7]}

Thanks
PSB
pls explain clearly. do you want to sort the keys ? or the dictionary values?
sort the keys means you want E1, then E2 and so on. Sort the values means for example, you want to sort [2,3,7,6] to become [2,3,6,7]
Mar 5 '07 #5
bvdet
2,851 Expert Mod 2GB
The following example shows the storing of the data and accessing the data from dictionary variable.The accessing of dictionary variable data is not a orderas what we are storing.

Is my understanding is correct?.Is it possible to retrievethe dictiondary variable data in a order,as what we store?

Thanks in advance
PSB
Expand|Select|Wrap|Line Numbers
  1. >>> dict1= {"E1":[1,2,6,5],"E2":[2,3,7,6],"E3":[3,4,8,7],"E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11]}
  2. >>> print dict1
  3. {'E5': [6, 7, 11, 10], 'E4': [5, 6, 10, 9], 'E6': [7, 8, 12, 11], 'E1': [1, 2, 6, 5], 'E3': [3, 4, 8, 7], 'E2': [2, 3, 7, 6]}
  4. >>> 
If you want to retrieve the data in the same order it was stored, you can use lists or tuples.
Expand|Select|Wrap|Line Numbers
  1. >>> keyList = ["E1", "E2", "E3", "E4"]
  2. >>> dataList = [[1,2,6,5],[2,3,7,6],[3,4,8,7],[5,6,10,9]]
  3. >>> for i, key in enumerate(keyList):
  4. ...     print '%s = %s' % (key, dataList[i])
  5. ...     
  6. E1 = [1, 2, 6, 5]
  7. E2 = [2, 3, 7, 6]
  8. E3 = [3, 4, 8, 7]
  9. E4 = [5, 6, 10, 9]
  10. >>> 
A dictionary stores data randomly. Data items can be appended to lists as they are acquired. Retrieving dictionary data from a sorted key list will not necessarily match the order of data acquisition.

Is this what you are trying to do?
Mar 5 '07 #6
psbasha
440 256MB
Hi,

Thanks for the reply

Yes,You are right.

PSB
Mar 5 '07 #7

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

Similar topics

7
by: Thomas Philips | last post by:
I want to print "1 spam 4 you" using a formatted string that gets its inputs from the dictionary d={'n1':1, 's1':'spam', 'n2':4}. To do so, I write >>> x="%('n1')d %('s1')s %('n2')d you" >>> x...
26
by: Alan Silver | last post by:
Hello, I have a server running Windows Server 2003, on which two of the web sites use the MegaBBS ASP forum software. Both sites suddenly developed the same error, which seems to be connected to...
15
by: barbaros | last post by:
Hello everybody, I need some advice on the following problem. I want to write a program (in C or C++) which will build a huge dictionary (hash array, if you prefer). The keys are strings, the...
3
by: Alex Maghen | last post by:
I want to create an object which is attached to the specific user session and I want to be able to access that object directly throughout the Pages, Page Controls, and Master Pages of the site. ...
8
by: Martin Pöpping | last post by:
Hello, I´ve implemented a Hashtable with Int-Keys and Double Values. Now I want to get the i-th Int-Key of my hashtable. How do I do that? I tried it like that: ICollection intKeys =...
2
by: Sike | last post by:
Hi everyone, I've been browsing this and a few other related newsgroups trying to get my head around this problem, and so far all the trails seem to go cold, without an acceptable solution being...
3
GTXY20
by: GTXY20 | last post by:
Hi All, I have been able to create a successful application with Python but I am being asked to create in VB. I have a text file with the following data: 1,a 1,b 1,c 2,a
1
by: Peter | last post by:
Hi, I have a Dictionary<key, valuewhich is accessed by three threads. One thread puts my value objects in the dictionary (occasionally), and also updates the contents of existing value objects -...
3
by: Mitko Haralanov | last post by:
I have a Python module that I have written using the C API and I am having a problem accessing a dictionary from that module. Here is what I have done: 1. In my init function I call module =...
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
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
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,...
0
jinu1996
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...
0
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...
0
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,...
0
isladogs
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.