Python base tuple, dictionary

Source: Internet
Author: User
Tags shallow copy

This article is divided into two parts, the first part is to discuss the nature of the tuple and some common methods, the second part is to discuss the nature of the dictionary and some common methods.

I. Nature and methods of tuples 1. The definition of a tuple:

Tuples are defined in the following way:

Tu= (12,23,[32,33],'dhue', Ture, (+,'re'))

The tuple's marker is "()". Tuples are a lot of properties and lists are the same, the two difference is that the elements in the list can be modified and deleted, but the elements in the tuple can not be modified and deleted (note that this refers to the first level element). In order to separate tuples from functions, we recommend adding "," after the last element of the tuple.

2. Properties of tuples: (1) elements of tuples cannot be modified

This nature should note that the element here is not modifiable, refers to the tuple of the first level element can not be modified, for example (12,34, ' str ', [12,22]) This tuple, each location of the data type cannot be changed, that is, the third string cannot be converted to a number, The fourth list changes to a different data type, but you can change the elements in the list to other elements.

(2) tuples can be traversed with for,while (tuples are ordered) (3) tuples can be indexed and slice operations (4) Tuples, list strings can be converted to each other 3. Method of the tuple class (1) count (value):

Calculates the number of occurrences of a value in a tuple

(2) index (Value,start=none,end=none):

Returns the position of the first occurrence of a value in a tuple, which can be specified.

Two. The nature and common methods of dictionaries 1. Dictionary definition:
1Dic={2     'Key1':'Hello',3' Key2 ':'Defe',4true:43233,5     ' Key3': [43,654,756,8            {9                'Key22': 675,Ten                'key54':'Hfuer', One                'hey44':(43,54) A            }], -     'Key4':'value' - } the Print(DIC)

A dictionary is defined by a key-value pair (Key,value), a key-value pair represents an element, and different key-value pairs are separated by "," separated by a ":" Between key values.

2. The nature of the dictionary (1) list, the dictionary cannot be the key value of the dictionary (2) The dictionary value value can be any type (3) The dictionary is unordered, so it is not possible to use the while loop (4) The dictionary is an iterative object, you can use a for loop, the for loop has the following four ways A. This loop is equivalent to the for item in Dic.keys (), which iterates through all keys.
 for inch dic:     Print (item)
B. Loop through all key values, which is the same as a loop method.
 for inch Dic.keys ():     Print (item)
C. Loop through all the value values.
 for inch dic.values ():     Print (item)
D. Loop through all the Key,value values.
 for inch Dic.items ():     Print (K,V)
(5) The dictionary can find the value corresponding to the key value according to the key value:
v=dic['key1')print(v)
(6) The dictionary is unordered, so the data index can be used to find the value (the index of the dictionary is the key value) (7) The dictionary can del Delete, delete the key value, the value of the key value corresponding value will also be deleted 2. Dictionary class (Dict) method: (1) Clear ():

Clear Dictionary

(2) Copy ():

Copy dictionary (shallow copy)

(3) Fromkeys ():
V=dict.fromkeys (['key','key2','key3 '],123)print(v)

Code Execution Results:

The method is used to initialize a dictionary, the element in the first parameter as the key value, if passed in a list, each element in the list will be the key value, the second parameter is the value of all key values, that is, all key values corresponding to the value value will be initialized to the values.

(4) Get (Key,d=none):

Returns the specified key value of the dictionary, and if the key value is not found in the dictionary, the specified data can be returned by specifying D, which returns none by default.

(5) Pop (Key,d=none):

Deletes the specified key value of the dictionary and returns the value of the value corresponding to the key value, and if the key value is not found, the return value of the method can be specified by D, which returns node by default.

(6) Popitem ():

Randomly deletes a key-value pair in the dictionary.

(7) SetDefault (Key,d=node):

Sets the default value for the key value, if the key value is present in the dictionary, returns the value of the value corresponding to the key value in the dictionary, and if the key value does not exist in the dictionary, the key value pair is added to the dictionary and the value of the key value is returned.

(8) Update ():
V=dict.fromkeys (['Key','Key2','Key3'],123)Print(v) v.update ({'Key': 12,'KKK':'Hshsh','Hdue': 454})Print(v)

Update the value of the dictionary key value, if there is a key value in the dictionary, update value, and if the key value does not exist in the dictionary, the key value pair is added to the dictionary.

Python base tuple, dictionary

Related Article

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.