Python3基础——序列类型
开头写给自己,To Myself:
很久以来,都想要学习一门编程语言,从去年选择了python开始,反反复复重新开始了N多遍,每一次不会超过俩星期。昨天无意间翻开自己去年记的学习笔记,不禁感叹想当年我曾那么用功,却未能坚持下来,着实可惜。这一次,我告诉自己,最后一次机会,必须坚持到底!must。。。。。。
Sequence Types — list, tuple, range
Common Sequence Operations
| Operation | Result | Notes |
|---|---|---|
| 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
Mutable Sequence Types
| Operation | Result | Notes |
|---|---|---|
| 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))
[]
列表、元组、字符串总结
共同点:
都可以通过索引得到每一个元素
默认索引值从0开始
可以用通过分片得到一个范围内的元素的集合
有很多共同的操作符(重复、拼接、成员关系操作符)
Python3基础——序列类型的更多相关文章
- Python3基础——字符串类型
Text Sequence Type - str(immutable) class str(object='') class str(object=b'', encoding='utf-8', err ...
- python基础知识01-数据类型和序列类型
%,取余 //,取整,向下取整,5//2 = 2. 一.变量类型 1.变量名不能以数字开头,不能纯数字,不要用汉字,可以用下划线开头 2.数值类型(int,float,bool,complex) ...
- python基础之数值类型与序列类型
Hello大家好,我是python学习者小杨同学,已经学习python有一段时间,今天将之前学习过的内容整理一番,在这与大家分享与交流,现在开始我们的python基础知识之旅吧. 数值类型与序列类型 ...
- python基础之序列类型的方法——字符串方法
python基础之序列类型的方法--字符串方法 Hello大家好,我是python学习者小杨同学,经过一段时间的沉淀(其实是偷懒不想更新),我终于想起了自己的博客账号,所以这次带来的是序列方法的后半部 ...
- python基础--数值类型和序列类型
Python中数值类型:int(整数),float(浮点数),True/False(布尔值,首字母必须大写) int:1 #任意整数 float:2.3 #小数 python赋值: a = ...
- python基础之序列类型的方法——列表&元组
Hello大家好,我是python学习者小杨同学,上次跟大家分享关于python的数值类型和序列类型,本次就承接上一节的内容,说一说序列类型的方法. 序列类型的方法,简单的来说就是四个字:增删改查.随 ...
- python基础——重访类型分类
python基础--重访类型分类 对象根据分类来共享操作:例如,字符串.列表和元组都共享诸如合并.长度和索引等序列操作. 只有可变对象(列表.字典和集合)可以原处修改:我们不能原处修改数字,字符串.元 ...
- Python 基本数据类型和序列类型
python 3.6.4 中,有9种数据类型: int, float, bool, complex, list, tuple, string, set, dict (1).int 整型,不可变 (2) ...
- python002 Python3 基础语法
python002 Python3 基础语法 编码默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -* ...
随机推荐
- Java学习笔记——java基础
2020.9.1 学习来自 https://www.liaoxuefeng.com/wiki/1252599548343744/1255883729079552 一.变量和数据类型 基本数据类型 基本 ...
- 蒲公英 · JELLY技术周刊 Vol.20: Vue3 极致优化——分析 Vue3 Compiler 告诉你为什么这么快
蒲公英 · JELLY技术周刊 Vol.20 性能优化是一条无尽的路,我们总是可以找到各种途径去提升体验,不论是响应时间还是按需加载,亦或是根据框架或者组件有针对性的优化都会是不错的方法.如果你在使用 ...
- Selenium使用cookis登录,并临时将cookis存储在本地【shelve数据库】
Python中自带了一个shelve库,可以帮助我们存储一些少量的数据. shelve数据库类似redis,是以[键值对]的方式进行数据的存储,有点像"字典"这种数据结构,存储在本 ...
- CocosCreator游戏开发(五)实现技能按钮
在上一篇中,已经顺利的实现了通过摇杆控件来控制角色移动的例子 这一篇内容中,主要来实现通过摇杆来操作技能施法位置的功能 代码效果如下: 在最初的想法中,我是想将摇杆与技能施法范围以及施法位置做成一个组 ...
- P1164 小A点菜(动态规划背包问题)
题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家--餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:"随便点". 题目描述 不过ui ...
- Java使用POI的SXSSFWorkbook与HSSFWorkbook导出复杂表头
一.HSSFWorkbook与SXSSFWorkbook的区别: HSSFWorkbook是对Excel2003以前的版本进行操作的,即后缀名为.xls SXSSFWorkbook时对Excel200 ...
- Azure Storage 系列(四)在.Net 上使用Table Storage
一,引言 今天我们就不多说废话了,直接进入正题,Azure Table Storage.开始内容之前,我们先介绍一下Azure Table Storage. 1,什么是Azure Table Stor ...
- idea导入spring源码
1.环境: Intellij idea 2018.2 gradle 4.10.2 spring framework:5.2.0 注意版本不符合可能会导致编译失败. 参考版本: 1.首先下载安装 Int ...
- Spring框架学习笔记(1)
Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...
- [Leetcode]225. 用队列实现栈 、剑指 Offer 09. 用两个栈实现队列
##225. 用队列实现栈 如题 ###题解 在push时候搞点事情:push时入队1,在把队2的元素一个个入队1,再交换队2和队1,保持队1除pushguocheng 始终为空. ###代码 cla ...