python in the list, tuple, dict, set Introduction --- Chen Yu Tong

Variables and objects variables and the objects themselves are connected (connection pointer object space), cited the establishment of the mapping between the variables and objects, which is the reference. References completed, to achieve the assignment. Variable through the object memory address points to the object, similar to the soft link

 

A variable assigned to the variable b, b point to the fact that an object variable pointing to a memory address

 

The following four elements are variable in the object type

 

 

List list is an ordered set, which you can add and remove elements at any time. Square brackets [] to represent the list, and the partition element such as a comma = [1,2,3]

 

() Function to get the number of elements of the list Len

 

List is an ordered collection of accessing elements in the list, pointed out that the list of names, and element index in [] in the index from zero.

Python be returned by the index within the list of elements negative reciprocal, such as a [-1] a [-2] were penultimate a second element, and so on

As can be sliced ​​by a colon indexing manner: 3, and between the elements of the output [14] The index 1 and index a list of available new slice to copy the list

Do not pay attention to the index out of bounds, otherwise it will error

+ Sign available end to end two lists of []

 

Add delete modify elements:

Modify elements in the list: a [1] = '321' can directly list index assignment

A list of elements added at the end: a.append ()

Insert elements: the index (i, (l, 2,3)) within the list of the index i is an element after insertion, the original index site element and back element a.insert plus 1

Delete end of the list of elements: a.pop ()

Delete the specified location elements: a.pop (i), i is the index position

Or you may delete the specified position of the element with del a [i] (del deleted as a function of a variable, the variable does not delete the data points)

When not know the location value according to delete: a.remove (), the value of the input elements does not exist in parentheses filled elements will be deleted error

The method by sorting the sort () Default is ascending, sort (reverse = True) descending

 

The list formula [x * x for x in range (1,10)]

After the for loop statement can be added if screening is determined as [x * x for x in range (1,10) if x% 2! = 0]

 

 

 

 

Another ordered collection called tuples tuple . and tuple list is very similar, with a small parentheses (without parentheses python tuples can be recognized, the recommended form parentheses), but can not modify Once initialized tuple

tuple is not changed, so the code is relatively safe, and compared to a list uses less memory. If possible, use tuple instead of the list as far as possible with the tuple

About tuple few other details:

Only when defining a tuple 1 elements, you must add a comma, or represent the element itself

Python tuple when displaying only one element, it will add a comma, lest you misunderstand brackets on the computing sense to mathematics such as (1)

 

 

 

 

 

 

 

Dictionary dict , dict Dictionary full name, also referred to in other languages map, key} in curly braces {- value (key-value) stored lookup faster. A key corresponds to only one value. With dict () {} initialization or

Wherein the key can only be immutable dict and the same key can not be the same for any two, and the value can be any object

如score={‘ming’:60,’gang’:70,’hong’:80}

Dictionary may be empty

Score a value of the dictionary in the dictionary access key [ 'ming']

Dictionary specified value except during initialization, the assignment r may be modified as score [ 'ming'] = 65 by key value

The key-value pairs can be added at any time score [ 'ding'] = 75 (dict disordered, regardless of the order and the order is added, different versions python reflected) in the dictionary

Or you can use del pop () to delete key-value pairs (due dict disordered, pop () must add the key, otherwise it will error)

Get All key-value pairs dict by score.items (), score.keys (), score.values ​​(), respectively, key, value

 

 

 

Collection set is an unordered sequence of elements will not be repeated, only the key corresponding to a value of not dict.

A set function is initialized by the braces {}, or set ()

By adding elements add () method to set

Or update () method to add elements, the parameters may be a list of tuples, dictionaries

() Method to remove elements by remove

Set also has a set of characteristics in a mathematical sense, the intersection can be set in a mathematical sense and other operations, the two set by & and | operators carry out the intersection or union operation

Guess you like

Origin www.cnblogs.com/niaocaizhou/p/12082274.html