Python combined data

Hello everyone, a blink of an eye, day has passed. Fans can have a few, thank you very much. Special thanks QQ friends called irid **** ent technical confidence in me, so I have to ask the question. Hope a lot of questions, like the point of a praise, plus a concern!

(Well, and in Minato words! Hasten to write the question! What a waste if it?)

Python conventional combined data into three categories:

  • Collection type:
    is a collection of elements, unordered between the elements, the only element present in the same set. Collection (set)

  • Sequence type:
    is an element vector, there is a precedence relationship between the elements, number access, non-exclusive between the elements. Typical sequence type is a string (str), a list (list), tuple (tuple)

  • Mapping type:
    a combination of "key" data items, each element is a key-value pair, denoted as (key, value). Mapping is a typical representative of the type of dictionary (dict)

We talk about the set:

As before, this time we talk about these types, the type has a corresponding transfer function, the set is the set (), the list is the list (), and so on and so forth. So as long as you keep in mind these types of English on the line.

Python language in a consistent set of concepts with a set of mathematical type, i.e. containing 0 or a random combination of a plurality of data items. Braces {} indicates that the index and there is no concept of location, the set of elements can be added or deleted dynamically. Due to the unique collection element, the collection type used to filter out duplicate elements.

Speaking of the index, we can talk about.

There are two index number system, we start with a positive incremental number, took the string 'Hello' as an example:

Ello H
0 2. 3. 1. 4
->
This is the key number is not the first one, is zero.

Reverse diminishing number:

H e l l o
-5 -4 -3 -2 -1
<——

There should be no problem.

May be employed [N: M] acquired substrings format, this operation is referred to as a slice image. [N: M] gets the string from N to M (but not including M) between successive substrings. Note that spaces are also accounted for a grid.

Here Insert Picture Description

We try more, I will remember.

Well serial number system finished, we go back to the set:

a = {1,3,2,4,6,5,9,4,1,2,4,1,5}

Is not filter out duplicate elements, the collection operator I can give you Kankan:

Here Insert Picture Description

Functions and methods for the collection:

Here Insert Picture Description

Most of these have talked about it, so it is very simple.

a = {1,2,5}
for i in a:
	print(i)

This is to traverse the collection.

Now we are talking about the type of sequence in the tuple (tuple).

Tuple consisting parentheses, Python tuple list is similar, except that the elements of the tuple can not be modified.
Tuples use parentheses (), the list using square brackets.
Tuple create very simple, only need to add elements in parentheses and separated by commas can be.

a = 3,
print(a,type(a))

Yes, this is a tuple. Tuple biggest feature is not modified, but can be combined:

a = (2,3,1)
b = (3,4,1)
c = a + b
print(c)

Tuples generally used to preserve some confidential documents more appropriate.

Now we revisit the list (list) sequence types. A list of very common, common than multi-sets and tuples (eds simple procedure, non-Web, developed Gui and the like).

List Central parentheses, in the list, can have any type.

A slice is a list of basic operations, for a list of fragments obtained, i.e., to obtain one or more elements. The result is a list slice type.

Slice mode:
or list variable [N: M: K]
slice type from N to obtain a list of M (containing no M) elements of the new list. When K is present, the slice type obtaining a list from N to M (not included M) to a list of elements K is the corresponding step size.

Here Insert Picture Description

This is the method of operation of the list, each very important, I hope you remember.

When it comes to increase, it is also deleted.
Python uses del keywords to delete, delete, will no longer have access, this keyword applies to all types.

For example:

a = [1,'2',3,'4']
del a
print(a)

Being given it, he did not say a definition, right?
For a list, del can also delete a clip:

del variable list [index number]
del variable list [index start: End Index]

Now we are talking about the type of mapping in the dictionary (dict):

Dictionary also by the large parentheses, but with a collection of different, but also very different.
Python language dictionaries using braces {} is established, each element is a key-value pair.

Use:
{Key 1: 1 value, the key 2: value of 2, ..., key n: n} values

Colon and key values ​​connected to different keys separated by commas. It may represent a set of braces, dictionaries and also having a set of similar properties, i.e. no order and can not be repeated between pairs.

Note that, the key is not in the dictionary is a variable type, such as a list, only immutable types, such as string type, tuple type.

We now look at a simple dictionary:

dic = {'a':1,'b':2,'c':3}

This is a dictionary, then the dictionary what use is it? Let's take a look at how to access the dictionary:

List type using the index position of the element sequence. As the dictionary element "on key" in the key index values ​​are, therefore, you can directly use the index key element of the relationship.

Variable dictionary [key]
use and assignment index = mating, the dictionary can be modified for each element.
Use braces to create the dictionary. By indexing and assignment fit, you can add elements to the dictionary.

The following is the dictionary method of operation:
Here Insert Picture Description

Note, d.get and d.pop return the default value refers to the default parameters, it can be ignored.

Dictionary have to traverse, in fact, all types can be traversed only method depends on traversal:

a = {1:'a',2:'b'}
for i in a:
    print(i)

You see, I will not say.

Well today is more than knowledge, I hope you remember that this is the basis of this foundation. Writing is not easy, we at least look at it. There are puzzled friend asked in the comments section, I'll try and answer. Partners interested in small, can add my QQ: 3418772261 . Our next goodbye!

Published 12 original articles · won praise 17 · views 10000 +

Guess you like

Origin blog.csdn.net/Persia_king/article/details/105069316