Dictionary indexes in Python

Source: Internet
Author: User

In Python, the following data types are supported: String, list, and sequence. They use integers as indexes. If you try to index other types, an error will occur.


[Python]
>>> List = [1, 2, 3]
>>> List [0]
1
>>> List ['one']
Traceback (most recent call last ):
File "<pyshell #34>", line 1, in <module>
List ['one']
TypeError: list indices must be integers, not str

>>> List = [1, 2, 3]
>>> List [0]
1
>>> List ['one']
Traceback (most recent call last ):
File "<pyshell #34>", line 1, in <module>
List ['one']
TypeError: list indices must be integers, not str
The dictionary index can be a string. In addition to this, it is very similar to its combination type. Of course, the dictionary index can also be an integer.


[Python]
>>> Dict1 = {'Mother ': 'Mom', 'Father ', 'Daddy '}
SyntaxError: invalid syntax
>>> Dict1 = {'Mother ': 'Mom', 'Father ': 'Daddy '}
>>> Dict1
{'Father ': 'Daddy', 'Mother ': 'Mama '}

>>> Dict1 = {'Mother ': 'Mom', 'Father ', 'Daddy '}
SyntaxError: invalid syntax
>>> Dict1 = {'Mother ': 'Mom', 'Father ': 'Daddy '}
>>> Dict1
{'Father ': 'Daddy', 'Mother ': 'mame'} we can also create an empty dictionary and add elements to it.


[Python]
>>> Eng2sp = {}
>>> Eng2sp ['one'] = 'uno'
>>> Eng2sp ['two'] = 'dos'
>>> Eng2sp
{'One': 'uno', 'two': 'dos '}

>>> Eng2sp = {}
>>> Eng2sp ['one'] = 'uno'
>>> Eng2sp ['two'] = 'dos'
>>> Eng2sp
{'One': 'uno', 'two': 'dos '} dictionary elements use commas as separators. Each element contains keys and key values, which are separated by colons.

Delete a dictionary


[Python]
>>> Inventory = {'append': 430, 'banas': 312, 'oranges': 525}
>>> Inventory
{'Banas': 312, 'append': 430, 'oranges': 525}
>>> Del inventory ['append']
>>> Inventory
{'Banas': 312, 'oranges': 525}

>>> Inventory = {'append': 430, 'banas': 312, 'oranges': 525}
>>> Inventory
{'Banas': 312, 'append': 430, 'oranges': 525}
>>> Del inventory ['append']
>>> Inventory
{'Banas': 312, 'oranges': 525}
To delete all elements, use the clear method.


[Python]
>>> Inventory. clear
<Built-in method clear of dict object at 0x0000000003289588>
>>> Inventory
{'Banas': 312, 'oranges': 525}
>>> Inventory. clear ()
>>> Inventory
{}

>>> Inventory. clear
<Built-in method clear of dict object at 0x0000000003289588>
>>> Inventory
{'Banas': 312, 'oranges': 525}
>>> Inventory. clear ()
>>> Inventory
{}
Returns the number of dictionary elements using the len function.


[Python]
>>> OS = {1: 'linux ', 2: 'windows '}
>>> Len (OS)
2

>>> OS = {1: 'linux ', 2: 'windows '}
>>> Len (OS)
2

The dictionary can be changed. If you want to modify the dictionary and keep the original backup, you need to use the dictionary copy method. Let's take a look at the example below:


[Python]
>>> Opp = {'up': 'down', 'right': 'left', 'true': 'false '}
>>> Alias = opp
>>> Alias
{'Right': 'left', 'up': 'lowdown ', 'true': 'false '}
>>> Id (opp)
53056584
>>> Id (alias)
53056584
>>> Other = opp. copy ()
>>> Other
{'Right': 'left', 'up': 'lowdown ', 'true': 'false '}
>>> Id (other)
52898824

>>> Opp = {'up': 'down', 'right': 'left', 'true': 'false '}
>>> Alias = opp
>>> Alias
{'Right': 'left', 'up': 'lowdown ', 'true': 'false '}
>>> Id (opp)
53056584
>>> Id (alias)
53056584
>>> Other = opp. copy ()
>>> Other
{'Right': 'left', 'up': 'lowdown ', 'true': 'false '}
>>> Id (other)
52898824

 

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.