Python学习笔记<list,tuple,range再探>
来源:互联网 发布:js 删除style属性 编辑:程序博客网 时间:2024/05/16 11:17
之前我也总结过一些用法,但是这些东西看起来不够公式化。今天无意间看到了python文档中得详细介绍。在这里搬运一下。
在python中,list、tuple、range都被成为sequence(序列)
str也被看做是特殊的sequence
list tuple range操作符表
我找重要的几个翻译一下:
专属于list的操作
有一些操作是list专属的(range、tuple作为不可变的序列)
比较重要的方法应该有:
在python中类似这样a=[0,1,2] b=a
的操作导致的结果是b只是a的一个引用(或者说别名),正确的方法是b=a.copy()
或者b=a[:]
list的构造方法
- 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’] and list( (1, 2, 3) ) returns [1, 2, 3] 分解字符串的好方法。字符串因为也是特殊的迭代器,所以可以转换成list。
iterable对象
stackoverflow中的回答
- 实现了 iter() 或 getitem() 方法的对象;(常用)
- 实现了 .next() 的可调用实体。
sort
sort(*, key=None, reverse=None)This method sorts the list in place, using only < comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state).
str.split()方法也返回的是一个list
这个方法可以很方便得用来分解字符串。
>>> string'son of bitch'>>> a=string.split()>>> a['son', 'of', 'bitch']
0 0
- Python学习笔记<list,tuple,range再探>
- python学习之 list tuple range
- Python学习笔记 - list和tuple
- Python学习笔记<list和tuple>
- 【Python学习笔记】使用list和tuple
- Python学习笔记:list,tuple和str
- python学习笔记使用list和tuple
- Python开发学习笔记(6) - List,Tuple
- 01-python学习笔记:list和tuple
- Python学习笔记1-List,tuple
- Python-List&Tuple学习
- 【Python笔记】list与tuple
- 王亟亟的Python学习之路(四)-循环,条件,Range,list和tuple
- Python的list,tuple学习
- Python学习-list 和 tuple
- Python学习笔记--表list, 元组tuple, 字典dict
- Python 学习笔记(二):List and Tuple
- python学习笔记(二)list和tuple类型
- 【kmp】算法总结
- 5-4 是否同一棵二叉搜索树 (25分)
- HDU-5122 K.Bro Sorting(贪心)
- pat1017Queueing at Bank (25)
- linux kexec 喂狗 方案 汇编代码
- Python学习笔记<list,tuple,range再探>
- 书籍共享
- Combination Sum III
- 求一个最长的串使得该串不包含任何禁止串为子串 AC自动机+DP +dfs判环 UVA 1399 - Puzzle
- 上下Div固定,中间div高度自适应(随浏览器高度变化而变化)
- C语言 switch和if..else的区别 以及枚举的作用
- UVA Poker Hands题解
- [深入理解Java虚拟机]第一章 走进Java
- ZOJ-3894-Chessgame【区间dp】