Python study notes (v) a combination of data types

  Before we learned to numeric types , including integer type, floating-point types and complex types , these types represent only a data, this represents a single data type called the basic data types . However, in the actual calculation, but where there are a lot of simultaneously processing a plurality of data, which is necessary to effectively organize and unified together represent a plurality of data, this type can represent a plurality of data is referred to a combination of data types .

A combined data type Overview

Composite data type is more capable of a plurality of same or different types of tissue together by a single data indicates that the more ordered, more easily. The relationship between the data, a combination of data types can be divided into three categories: a sequence type, set type and the map type .

Sequence type is an element vector, there is a relationship between elements has, by serial number access, non-exclusive between the elements.

Collection type is a type of elements, unordered, the same elements between which the unique presence in the collection.

Map type is a combination of "key" data items, each element is a key-value pair, denoted as (key, value).

 

1. Type sequence

Sequence type is a bunch of elements of a vector, there is a precedence relationship between elements accessible through the serial number.

Python There are many types of data sequence type, which is more important str (string), tuple (tuple) List (list) can be viewed as an ordered sequence of single characters belonging to sequence types.

There are 12 sequence types of operators and functions common

 

Operators Description
x in s If x is an element yes, it returns True, whether those returns False
x not in s If x is not an element s, returns True, otherwise False
s + t T S and is connected
s * n or n * s The replication sequence s n times
s[i] Index, return sequence of i-th element
s[i:j] Fragment, comprising the sequence returns subsequence j to s i-th element (j-th element is not included)
s[i:j:k] Step fragment, comprising the sub-sequence returns s to the j-th element of the i k is the number of steps to
only (a) S sequence number of elements (length)
min (s) The minimum element sequence s
max(s) The largest element in the sequence s
s.index(x(,i[,j])) Sequence s i from the start position to the position of the element j in the first occurrence of x
s.count(x) The total number of x sequence appears

Tuple (tuple) is a special type of sequence type, as it can not be modified once created. Python tuples in the parentheses and comma (optionally) be expressed as:

>>>creature = "cat", "dog", "tiger", "bear"
       print(creature)
>>>('cat', 'dog', 'tiger', 'bear')

>>>creature = "cat", "dog", "tiger", "bear"
  all=(1,2,456,creature)  #元组中包含元组creature
  print(all)
  print(all[2])
  print(all[-1][2])
>>>(1, 2, 456, ('cat', 'dog', 'tiger', 'bear'))
>>>456
>>>tiger

Wherein, a tuple can be used as elements of another tuple, multilevel indexing may be employed to obtain information, such as the above tuple contains all tuples Creature, may obtain a corresponding element values ​​with all [-1] [2].

 

In addition to tuple expression of fixed data, but also used in the following three cases: the function returns the value of the multi, multi-variable synchronous assignment, loop through , for example:

>>> DEF FUNC (X):           # Function Return Value multiple       
           return X, X * X;    

>>> A, B = 'Dog', 'CAT' # multi-variable synchronous assignment

>>> import math # looping through
  for x , y in ((1,0), (2,5), (3,8)):
    from print (math.hypot (x, y) ) # plurality of coordinate values to find the origin

 

2. Collection Type

Collection type is consistent with the concept of mathematical set, i.e., a combination comprising zero or more random data items.

Set of elements is not repeated, only the element type is a fixed data type, such set, floating point, string, and other tuples, lists, dictionaries, and the set of data types are variable type itself, can not appear as a set of elements .

Since the combination set is unordered, and it does not have the concept of index positions, not fragment, elements in the set can be added and deleted dynamically. It represents a set of braces ({}), may be generated using a set assignment, for example:

>>>S={100,200,(666,25),"YES",458}
        print(S)
>>>{100, 200, 458, 'YES', (666, 25)}

As can be seen from the above, since the elements of the set is unordered, the print order of the definitions set can be different.

There are 10 types of set operators

Operators Description
S-T 或 S.difference(T) It returns a new set, comprising a set of elements in S but not in the set T
S-=T 或 S.difference_update(T) Update set S, the set S comprising elements but not in the set T
S & T 或 S.intersection(T) It returns a new collection, including at the same time elements of the set S and T
S&=T 或 S.intersection_update(T) Update set S, while the collection element including S and T
s=^T 或 S.symmetric-difference(T) It returns a new collection, including elements of the set S and T, but not at the same time the elements therein
s=^T 或 S.symmetric-difference_update(T) Updating the set S, the elements comprising the set S and T, but not at the same time the elements therein
S | T or S.union (T) It returns a new set, comprising all the elements of the set S and T
S=|T 或 S.update(T) Update the collection S, including all elements of set S and T
S<=T 或 S.update(T) S and T are the same or, if S is a subset of T returns True, otherwise returns False, may be S> T S is determined whether subset of T
S> = T or S.issuperset (T) S and T are the same or, if S T is a superset returns True, otherwise returns False, may be S> T S is determined whether T is true superset

The biggest difference from other types of collection types that it does not contain repetitive elements, and therefore, when a need to re-dimensional data or data duplication process is generally accomplished through the set.

 

3. Mapping Types 

Map type is a combination of "key" data items, each element is a key-value pair, i.e., elements are (key, value), between the elements are unordered.

Key (key) Represents a property can also be understood as a category or item, value (value) is the content attribute value pairs portrayed a property and its value. Mapping the key structural relationship, for storing and expression pair.

In Python, the type of mapping mainly to the dictionary (dict).

 

Second, a list of types and operations

A list that contains an ordered sequence of 0 or more object reference, no length limit, are free to add or delete elements, use and flexible.

1.列表类型的概念

列表(list)是包含0个或多个对象引用的有序序列,属于序列类型。与元组不同,列表的长度和内容都是可变的,可自由对列表中的数据项进行增加、删除或替换

2.列表类型的操作

函数或方法 描述
ls[i] = x 替换类表 ls 第 i 数据项为 x
ls[i:j] =l t 用列表 lt 替换列表 ls 中第 i 到第 j 项数据(不含第 j 项)
ls[i:j:k] = lt 用列表 lt 替换列表 ls 中第 i 到第j项以k为步数的数据(不含第j项)
del ls[i:j] 删除列表 ls 第i到第 j 项数据,等价于ls[i:j]=[]
del ls[i:j:k] 删除列表 ls 第 i 到第 j 项以 k 为步数的数据
ls += lt 或 ls.extend(lt) 将列表 lt 元素增加到列表 ls 中
ls *= n 更新列表 ls,其元素重复 n 次
ls.append(x) 在列表 s 最后增加一个元素 x
ls.clear() 删除 ls 中的所有元素
ls.copy() 生成一个新列表,复制 ls 中的所有元素
ls.insert(i,x) 在列表 ls 的第 i 位置增加元素 x
ls.pop(i) 将列表 ls 中的第 i 项元素取出并删除该元素
ls.remove(x) 将列表中出现的第一个元素 x 删除
ls.reserve(x) 列表 ls 中的元素反转

 

 

三、字典类型和操作

列表是存储和检索数据的有序序列。当访问列表中的元素时,可以通过整数的索引来查找它,这个索引时元素在列表中的符号,列表的索引模式是“<整数序号>查找<被索引内容>”

 

1.字典类型的概念

通过任意键信息查找一组数据中值信息的过程叫做映射,Python语言中通过字典实现映射。Python语言中的字典可以通过大括号({})建立,建立模式如下:

{<键1>:<值1>, <键2>:<值2>, ... ,<键n>:<值n>}

其中,键和值通过冒号连接,不同键值对通过逗号隔开。

例如:

>>>Dic={"中国":"北京","美国":"华盛顿","法国":"巴黎"}
       print(Dic)
       print(Dic["中国"])    
>>>{'中国': '北京', '美国': '华盛顿', '法国': '巴黎'}
>>>中国

一般来说,字典中键值对的访问模式如下,采用中括号格式:

<值> = <字典变量> [<键>]

 

2.字典类型的操作

与列表类似Python字典也有非常灵活的操作方法。使用大括号可以创建字典,并指定初始值,通过中括号可以增加新的元素,例如:

>>>Dic={"中国":"北京","美国":"华盛顿","法国":"巴黎"}
       Dic{"英国"}="伦敦"
       print(Dic)
>>>{"中国":"北京","美国":"华盛顿","法国":"巴黎","英国":"伦敦"}

注意:直接使用大括号({})生成一个空的字典。生成空集合需要使用函数set()。

字典类型的函数和方法:

函数和方法 描     述
<d>.keys() 返回所有的键信息
<d>.values() 返回所有的值信息 
<d>.items() 返回所有的键值对 
<d>.get(<key>,<default>) 键存在则返回相应值,否则返回默认值 
<d>.pop(<key>,<default>) 键存在则返回返回值,同时删除键值对,否则返回默认值 
<d>.popitem() 随机从字典中取出一个键值对,以元组(key,value)形式返回 
<d>.clear()  删除所有的键值对 
 del<d>[<key>]  删除字典中某一个键值对 
 <key>in<d> 如果键在字典中返回True,否则返回False 

与其他组合类型一样,字典可以通过for-in语句对其元素进行遍历,例如:

>>>Dic={"中国":"北京","美国":"华盛顿","法国":"巴黎"}
       for i in Dic:
           print(i)
>>>中国
   美国
   法国

所以,字典有以下一些基本原则:

(1)字典是一个键值对的集合,该集合以键为索引,一个键信息只对应一个值信息。

(2)字典中元素以键信息为索引访问。

(3)字典长度是可变的,可以通过对键信息赋值实现增加或修改键值对。

 

Guess you like

Origin www.cnblogs.com/Y-xp/p/11733995.html