Python3基础——序列类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python3基础——序列类型相关的知识,希望对你有一定的参考价值。

 开头写给自己,To Myself:

很久以来,都想要学习一门编程语言,从去年选择了python开始,反反复复重新开始了N多遍,每一次不会超过俩星期。昨天无意间翻开自己去年记的学习笔记,不禁感叹想当年我曾那么用功,却未能坚持下来,着实可惜。这一次,我告诉自己,最后一次机会,必须坚持到底!must。。。。。。

Sequence Types — list, tuple, range

 

Common Sequence Operations

OperationResultNotes
x in s True if an item of s is equal to x, else False——关系操作符 (1)
x not in s False if an item of s is equal to x, else True——关系操作符 (1)
s + t the concatenation of s and t——连接操作符 (6)(7)
s * n or n * s equivalent to adding s to itself n times——重复操作符 (2)(7)
s[i] ith item of s, origin 0——获取下标位i的元素,下标从0开始 (3)
s[i:j] slice of s from i to j——切片拷贝 (3)(4)
s[i:j:k] slice of s from i to j with step k (3)(5)
len(s) length of s  
min(s) smallest item of s  
max(s) largest item of s  
s.index(x[, i[, j]])

index of the first occurrence of x in s (at or after index i and before index j)

元素x在s中最早出现的位置下标,可以设置下标所在范围的起始位置

(8)
s.count(x) total number of occurrences of x in s——元素x在序列s中出现的次数  

 

Immutable Sequence Types

hash()

Mutable Sequence Types

OperationResultNotes
s[i] = x item i of s is replaced by x  
s[i:j] = t slice of s from i to j is replaced by the contents of the iterable t  
del s[i:j] same as s[i:j] = []  
s[i:j:k] = t the elements of s[i:j:k] are replaced by those of t (1)
del s[i:j:k] removes the elements of s[i:j:k] from the list  
s.append(x) appends x to the end of the sequence (same as s[len(s):len(s)] = [x])  
s.clear() removes all items from s (same as del s[:]) (5)
s.copy() creates a shallow copy of s (same as s[:]) (5)
s.extend(t) or s += t extends s with the contents of t (for the most part the same as s[len(s):len(s)] = t)  
s *= n updates s with its contents repeated n times (6)
s.insert(i, x) inserts x into s at the index given by i (same as s[i:i] = [x])  
s.pop([i]) retrieves the item at i and also removes it from s (2)
s.remove(x) remove the first item from s where s[i] == x (3)
s.reverse() reverses the items of s in place (4)

Lists

1、创建列表的方法:

  • Using a pair of square brackets to denote the empty list: []
  • Using square brackets, separating items with commas: [a], [a, b, c]
  • Using a list comprehension: [x for x in iterable]
  • Using the type constructor: list() or list(iterable)

    list(‘abc‘) returns [‘a‘, ‘b‘, ‘c‘]

    list( (1, 2, 3) ) returns [1, 2, 3]

2、除了以上方法外,列表还支持sort()方法

  sort(*, key=None, reverse=None)

       对列表进行排序,默认是按照从小到大的顺序排列

Tuples

1、创建元组的方法:

  • Using a pair of parentheses to denote the empty tuple: ()
  • Using a trailing comma for a singleton tuple: a, or (a,)
  • Separating items with commas: a, b, c or (a, b, c)
  • Using the tuple() built-in: tuple() or tuple(iterable)

 

       For example, tuple(‘abc‘) returns (‘a‘, ‘b‘, ‘c‘) and tuple( [1, 2, 3] ) returns (1, 2, 3)

注意:当你要创建的元组只有一个元素时,必须带逗号

Ranges

代表一组不可变的数字序列,主要用于for循环

class range(stop)

class range(start, stop[, step])

参数必须为整数,step步幅默认为1,start开始参数默认为0、

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]

以上是关于Python3基础——序列类型的主要内容,如果未能解决你的问题,请参考以下文章

Python3-随笔目录

Python基础面试题

学习笔记:python3,代码片段(2017)

scrapy主动退出爬虫的代码片段(python3)

scrapy按顺序启动多个爬虫代码片段(python3)

Pyhon基础知识之Json序列化与反序列化