Python Study Notes - Data structure

First, standard data types

1.1 immutable data

Number(Number)
String(string)
Tuple(tuple)

1.2 Variable Data

List(List)
Set(collection)
Dictionary(dictionary)

Second, immutable data

2.1 Number (digital)

Support Type

  • int
  • float
  • bool( In Python2 is not Boolean, which represents a digital 0 False, represented by 1. True. Python3 to in the True and False is defined as a keyword, but the value 0 or 1 and they may be and digital phase was added, and can use and, orand notoperation. )
  • complex(plural)

Usage Profile

Type judgment
a, b, c, d = 20, 5.5, True, 4+3j
print(type(a), type(b), type(c), type(d))
<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>
Deleting objects
del var
note
  • 1, Python may for multiple variable assignments, such as a, b = 1, 2.
  • 2, a variable may be directed by assigning different types of objects.
  • 3, comprises two division values ​​operators: / returns a floating point number, // returns an integer.
  • 4, upon mixing calculations, Python integer will be converted into floating-point numbers.

2.2 String (String)

Python in single quotes' or double quotes "quotes, while the backslash \ escape special characters
taken string syntax is as follows:

变量[头下标:尾下标]

Index value to the starting value 0, -1 from the end of the start position.
Here Insert Picture Description

note
  • 1, can be used to escape the backslash, backslash escapes allow the use of r can not happen, r'\'will output a backslash ().
  • 2, the + operator strings can be connected together, with the * operator repeats.
  • 3, Python strings have two indexing methods, starting with 0 from left to right, right to left to begin -1.
  • 4, the string can not be changed in Python. Assigning a value to an index location, such as word [0] = 'm' will cause an error.

2.3 Tuple (tuple)

Tuple (tuple) and a list of similar, except that the elements of the tuple can not be modified. Tuple written in parentheses (), the elements separated by commas.

#!/usr/bin/python3
 
tuple = ( 'abcd', 786 , 2.23, 'runoob', 70.2  )
tinytuple = (123, 'runoob')

print (tuple)             # 输出完整元组
print (tuple[0])          # 输出元组的第一个元素
print (tuple[1:3])        # 输出从第二个元素开始到第三个元素
print (tuple[2:])         # 输出从第三个元素开始的所有元素
print (tinytuple * 2)     # 输出两次元组
print (tuple + tinytuple) # 连接元组

Although the tuple elements can not be changed, but it can contain a variable object, such as lista list, contains the listlist is variable.

>>> t = ('a', 'b', ['A', 'B'])
>>> t[2][0] = 'X'
>>> t[2][1] = 'Y'
>>> t
('a', 'b', ['X', 'Y'])

Tuple structure contains zero or one element of the special, so there is some additional syntax rules:

tup1 = ()    # 空元组
tup2 = (20,) # 一个元素,需要在元素后添加逗号
note
  • 1, and the string like element of the tuple can not be modified.
  • 2, a tuple can be indexed and sliced, as a method.
  • 3, note that a special syntax rules configured tuple contains 0 or 1 elements.
  • 4, a tuple can be spliced ​​using the + operator.

Third, variable data

3.1 List (List)

You can complete the list data structure to achieve most of the collection class. Type elements in the list may not be identical, which supports numbers, strings may even contain a list (called nesting).
List is written between the [] brackets, comma-separated list of elements. And like strings, and indexing the list can also be taken after the list is taken returns a new list containing required elements.

变量[头下标:尾下标]

Here Insert Picture Description

Basic use
  • List of operations
len(list) 	 		   # 获取长度
append(data)  		   # 在末尾插入数据
insert(address,data)   # 在指定位置插入元素
pop(data)			   # 删除末尾数据
  • Two-dimensional list
N = [[0]*10 for i in range(10)]
note
  • 1, List written between square brackets, elements separated by commas.
  • 2, and the same string, list can be indexed and sliced.
  • 3, List can be spliced ​​using the + operator.
  • 4, List the elements can be changed.

3.2 Set (collection)

It is a collection of a number of different forms or the size of the entire composition, object or set of objects constituting the elements or members referred to.
The basic function is to test and remove duplicate membership elements, repeated elements are automatically filters in the set.
You can use braces { }or set()create aggregate functions, note: create an empty set must be used set()instead of { }, as { }is used to create an empty dictionary, recommend general use set(), when you add a list, using {}an error.

Basic use
add(data)		# 添加元素,已有元素,添加不会有效果
remove(data)	# 删除元素
set & set		# 交集
set | set		# 并集

3.3 Dictionary (dictionary)

Dictionary (Dictionary) is another useful Python built-in data types, similar to the map.
List is an ordered collection of objects, dictionaries are unordered collections of objects. The difference between the two is that: among the elements of the dictionary is accessed by a key, rather than by shifting access.
A dictionary mapping type, a dictionary { }ID, which is an unordered 键(key) : 值(value)collection.
Key (key) must be immutable.
In the same dictionary, the key (key) must be unique.

Basic use
get(key,val)	#获取key的值,若存在返回值,不存在则设置了val返回val,无设置返回None
pop(key)		#删除key
list and dict difference
  • dict
    1, to find and insert the fast, the key will not increase slowed;
    2, take a lot of memory, memory and more waste.
  • List
    1, find and insert a time increases the element increases;
    2, small footprint, waste very little memory.
note
  • 1, is a dictionary mapping type, whose elements are pairs.
  • 2, the dictionary of keywords must be immutable and can not be repeated.
  • 3, {} used to create an empty dictionary.

Since finishing
rookie tutorial
Liao Xuefeng's official website

Guess you like

Origin blog.csdn.net/Kalenee/article/details/85600296