Python - How to access data of the first type

NumberTextSet3 = {"ten": 10, "hundred": 100, "thousand": 1000, "million": 1000000, "billion": 1000000000, "trillion": 1000000000000} 

In this dictionary, I can access the number 1000000000000 using NumberTextSet3["trillion"] . But how can I access the last word in the dictionary, perhaps, for example: NumberTextSet3[-1] and return it to "trillion" ?

+5
source share
6 answers

There is no last word in the dictionary.

To check this, try

 print NumberTextSet3 

You will get another ordered result at another time.

You can slightly change your data structure to [("ten",10),("hundred",100),...]

Now you can use it with an index.

For instance,

 a=[("ten",10),("hundred",100)] print a[0][0] print a[1][1] 

Output:

 ten 100 

You can also use OrderedDict

+1
source

I would use OrderedDict as follows:

 from collections import OrderedDict NumberTextSet3 = OrderedDict([("ten", 10), ("hundred", 100), ("thousand", 1000), ("million", 1000000), ("billion", 1000000000), ("trillion", 1000000000000)]) # On Python2 this will work: print NumberTextSet3.keys()[-1] # This is a little bit longer but will work in Python2 and Python3: print list(NumberTextSet3.keys())[-1] 
+1
source

The dictionary is not ordered, so it does not have such a thing as the "first" or "last" entry. You can access a specific key as follows:

 NumberTextSet3["trillion"] 

or

 NumberTextSet3.get("trillion") 

If you want to access the list of keys you can do:

 NumberTextSet3.keys() 

that return to my computer: ['billion', 'trillion', 'ten', 'thousand', 'million', 'hundred']

and, as you see, there is no "order."

0
source

This may work, although it is not guaranteed, since the dictionaries are not ordered.

 NumberTextSet3.items()[-1][0] 

Edit: you can use the list of tuples instead (below); in this case, the same method will work, since items() gives a dict in key / value [tuple] pairs.

 NumberTextSet3 = (("ten", 10), ("hundred", 100), ("thousand", 1000), ("million", 1000000), ("billion", 1000000000), ("trillion", 1000000000000)) 
0
source

You will need to extract all the records and then sort them:

 import operator NumberTextSet3 = {"ten": 10, "hundred": 100, "thousand": 1000, "million": 1000000, "billion": 1000000000, "trillion": 1000000000000} values = sorted(NumberTextSet3.items(), key=operator.itemgetter(1)) print values[-1][0] 

Donation:

 trillion 

If you want to perform a reverse lookup, you can create a reverse lookup as follows and use this:

 InverseNumberTextSet3 = {v: k for k, v in NumberTextSet3.items()} print InverseNumberTextSet3[1000000000000] 

Providing:

 trillion 
0
source

Python dictionaries do not have an order guarantee.

This is because the dictionary is a hash map, and the hash map has its own key order determined by the hash function (which must be pseudo-random).

So, although you can access the first and last items in the dictionary ( NumberTextSet3.items()[0] and NumberTextSet3.items()[-1] , respectively), they probably do not correspond to the order in which you created the dictionary .

However, the standard library provides collections.OrderedDict , which preserves the insertion order, hence

 from collections import OrderedDict NumberTextSet3 = OrderedDict(( ("ten", 10), ("hundred", 100), ("thousand", 1000), ("million", 1000000), ("billion", 1000000000), ("trillion", 1000000000000) )) print(list(NumberTextSet3.keys())) => ['ten', 'hundred', 'thousand', 'million', 'billion', 'trillion'] print(list(dict(NumberTextSet3).keys())) => ['million', 'hundred', 'ten', 'billion', 'thousand', 'trillion'] 

Remember that I am changing the dictionary literal to a list of tuples. Using a dictionary literal would not work, since it would create a temporary, disordered dictionary and pass it to an OrderedDict to freeze the (quasi-random) order.

0
source

Source: https://habr.com/ru/post/1232678/


All Articles