python序列数据类型_Python 数据类型 之 序列类型
序列:表示索引为非负整数的有序对象集合,所有序列都支持迭代
序列类型有:字符串,列表,元组 三种
字符串也是一种序列
列表和元组是任意python对象的序列或叫有序集合
字符串和元组不可变序列,列表支持插入、删除和替换元素
序列类型的通用操作方法:
1. 索引运算。s[i] [i]可以使用负数,即倒着取值
2. 切片运算。s[i:j] ,切片后会生成新的对象
3. 扩展切片。s[i:j:stride],指定步长值
obj[1:] , obj[-2:-1],obj[0:6:2]
字符串 str
常用操作:
索引
切片
移除空白 obj.strip()
分割 obj.split()
长度 len(obj) , obj.__len__()
返回索引 obj.index(),obj.find()
输出位置 obj.center(),obj.ljust(),obj.rjust()
...
__len__(self, /)
Return len(self).| capitalize(...) '''首字母大写'''
| S.capitalize() ->str|
|Return a capitalized version of S, i.e. make the first character| have upper case andthe rest lower case.>>> 'test string'.capitalize()'Test string'
|center(...)| S.center(width[, fillchar]) ->str| Return S centered in a string of length width. Padding is
| done using the specified fill character (default isa space)>>> print(8*'#')########
>>> 'test'.center(20,'*')'********test********'
|ljust(...)| S.ljust(width[, fillchar]) ->str>>> 'test'.ljust(10,'<')
Out[61]: 'test <<<<<'
|rjust(...)| S.rjust(width[, fillchar]) ->str>>> 'test'.rjust(10,'>')
Out[59]: '>>>>> test'
| count(...) '''统计字符出现的个数'''
| S.count(sub[, start[, end]]) ->int>>> 'test string'.count('s')
In [8]: 'test string'.count('s',1,4)| encode(...) '''指定字符编码'''
| S.encode(encoding='utf-8', errors='strict') ->bytes|
| Encode S using the codec registered for encoding. Default encoding is 'utf-8'.>>> '中文'.encode('gbk')
b'\xd6\xd0\xce\xc4'
'''utf8可以直接转成gbk(内部实现通过unicode)'''
| endswith(...) '''判断是否以某个字符后缀结束'''
| S.endswith(suffix[, start[, end]]) ->bool|
| Return True ifS ends with the specified suffix, False otherwise.>>> 'test string'.endswith('t',1,4)
True|startswith(...)| S.startswith(prefix[, start[, end]]) ->bool| expandtabs(...) '''将tab转换成空格'''
| S.expandtabs(tabsize=8) ->str>>> 'te\tst'.expandtabs()'te st'
|format(...)| S.format(*args, **kwargs) ->str>>> info='my name is {0}, sex {1}'
>>> info.format('Jack','male')'my name is Jack, sex male'
>>> info='my name is {Name}, sex {Sex}'
>>> info.format(Name='Lucy',Sex='female')'my name is Lucy, sex female'
| find(...) '''返回字符的索引, 如有多个,只返回第一个字符的索引,不存在返回-1'''
| S.find(sub[, start[, end]]) ->int|
| Return the lowest index in S where substring sub isfound,| such that sub iscontained within S[start:end].| Return -1on failure.>>> 'test string'.find('t',2,6)| index(...) '''返回字符的索引, 如有多个,只返回第一个字符的索引,不存则报异常'''
| S.index(sub[, start[, end]]) ->int|
| Like S.find() but raise ValueError when the substring is notfound.>>> 'test string'.index('a')
ValueError: substringnotfound>>> 'test string'.find('a')-1
|rfind(...)| S.rfind(sub[, start[, end]]) ->int|rindex(...)| S.rindex(sub[, start[, end]]) ->int|isalnum(...)| S.isalnum() ->bool|isalpha(...)| S.isalpha() ->bool|isdecimal(...)| S.isdecimal() ->bool|isdigit(...)| S.isdigit() ->bool|islower(...)| S.islower() ->bool| Return True if all cased characters in S are lowercase and there is
| at least one cased character inS, False otherwise.|isupper(...)| S.isupper() ->bool| Return True if all cased characters in S are uppercase and there is
| at least one cased character inS, False otherwise.>>> 'TEST STRING'.isupper()
True|upper(...)| S.upper() ->str| istitle(...) '''判断是否所有单词首字母大写'''
| S.istitle() ->bool|
| Return True if S is a titlecased string and there isat least one| character inS>>> 'Test String'.istitle()
True|title(...)| S.title() ->str|
|Return a titlecased version of S>>> 'test string'.title()'Test String'
| join(...) '''指定连接符将序列的元素连接起来'''
| S.join(iterable) ->str|
| Return a string which is the concatenation of the strings in the iterable. The separator between elements isS.>>> ls=['a','b','c']>>> '-'.join(ls)'a-b-c'
| split(...) '''将字符串分割,形成列表, 不指定分隔符默认为空格'''
| S.split(sep=None, maxsplit=-1) ->list of strings|
| Return a list of the words inS, using sep as the| delimiter string. If sep is notspecified, any whitespace string| isa separator.>>> 'test string'.split()
['test', 'string']>>> 'test=string'.split('=')
['test', 'string']>>> 'this is test string'.split(maxsplit=2)
['this', 'is', 'test string']| rsplit(...) '''从右到左进行分割'''
| S.rsplit(sep=None, maxsplit=-1) ->list of strings| strip(...) '''去除首尾的字符,默认为空格'''
| S.strip([chars]) ->str|
| Return a copy of the string S with leading andtrailing|whitespace removed.| If chars is given and not None, remove characters inchars instead.>>> 'test string'.strip()'test string
>>> '### test string ###'.strip('#')'test string'
>>> '### test string ###'.lstrip('#')'test string ###
>>> '### test string ###'.rstrip('#')'### test string'
|partition(...)| S.partition(sep) ->(head, sep, tail)|
| Search for the separator sep in S, and return the part before it, the separator itself, and the part after it. If the separator is not
| found, return S andtwo empty strings.>>> '### test ***'.partition('test')
('###', 'test', '***')|rpartition(...)| S.rpartition(sep) ->(head, sep, tail)|replace(...)| S.replace(old, new[, count]) ->str>>> 'abc'.replace('b','2')'a2c'
| swapcase(...) '''大写转小写,小写转大写'''
| S.swapcase() ->str|translate(...)| S.translate(table) ->str| maketrans(x, y=None, z=None, /)| Return a translation table usable forstr.translate().>>> intab='abcd'
>>> outtab='1234'
>>> table=str.maketrans(intab,outtab)>>> 'abcdefg'.translate(table)'1234efg'
help(str)
列表 list
创建列表:
ls=['a','b','c'] 或 list(['a','b','c']) 或 list(('a','b','c'))
常用操作:
索引
切片
追加 obj.append(),obj.extend()
插入 obj.insert()
删除 __delitem__(),obj.remove(),obj.pop()
长度 len(obj)
返回索引 obj.index()
循环 for,while
包含 in
注意:append(),extend(),insert(),remove(),pop() 等都是直接修改列表,不会产生新的对象,不能对列表操作这些方法后赋值给另外一个变量,例如:ls2=ls1.append(),如要赋值可以使用__add__()
| __contains__(self, key, /)| Return key inself.| __delitem__(self, key, /) '''删除指定位置的元素'''
|Delete self[key].>>> ls=['a','b','c']>>> ls.__delitem__(1)>>> print(ls)
['a', 'c']| __len__(self, /) '''统计列表的长度(即元素的个数)'''
|Return len(self).| append(...) '''在末尾追加单个元素'''
| L.append(object) -> None --append object to end|clear(...)| L.clear() -> None -- remove all items fromL| copy(...) '''浅拷贝'''
| L.copy() -> list --a shallow copy of L| count(...) '''统计某个元素的出现个数'''
| L.count(value) -> integer -- returnnumber of occurrences of value>>> ls=['h','e','l','l','o']>>> ls.count('l')2
| extend(...) '''从序列里扩展元素'''
| L.extend(iterable) -> None -- extend list by appending elements fromthe iterable>>> ls=['h','e','l','l','o']>>> ls.extend('world')>>> print(ls)
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']| index(...) '''获取某个元素的索引,如有多个,只返回第一个'''
| L.index(value, [start, [stop]]) -> integer -- returnfirst index of value.| Raises ValueError if the value is notpresent| insert(...) '''在指定的索引迁插入'''
| L.insert(index, object) --insert object before index| pop(...) '''删除指定位置的元素并获取到这个元素,默认为最后一个元素'''
| L.pop([index]) -> item -- remove and returnitem at index (default last).| Raises IndexError if list is empty or index isout of range.>>> ls=['a','b','c',]>>> ls.pop(2)'c'
>>>ls.pop()'b'
| remove(...) '''删除指定的元素'''
| L.remove(value) -> None --remove first occurrence of value.| Raises ValueError if the value is notpresent.>>> ls=['a','b','c',]>>> ls.remove('c')| reverse(...) '''倒序'''
| L.reverse() -- reverse *IN PLACE*
>>> ls=['a','b','c']>>>ls.reverse()>>> print(ls)
['c', 'b', 'a']| sort(...) '''对列表内的元素进行排序, 可以指定key'''
| L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
>>> ls = ['Chr1-10.txt','Chr1-1.txt','Chr1-2.txt','Chr1-14.txt','Chr1-3.txt','Chr1-20.txt','Chr1-5.txt']>>> ls.sort(key=lambda d : int(d.split('-')[-1].split('.')[0]))>>> print(ls)
['Chr1-1.txt', 'Chr1-2.txt', 'Chr1-3.txt', 'Chr1-5.txt', 'Chr1-10.txt', 'Chr1-14.txt', 'Chr1-20.txt']
help(list)
元组 tuple
创建元组:
tp=('a','b','c') 或 tp=tuple((1,2,3)) 或 tp=tuple(['x','y','z'])
常用操作:
索引
切片
迭代 for / while
长度 len(obj)
包含 in / not in
注意:元组本身是不可变对象 ,长度固定,所以代码更安全。 所谓的“不变”是指元组的每个元素指向永远不变,元组本身不可变,但元组内嵌套了可变类型的元素,此元素的修改不会返回新元组.
即:
元组的元素只读,不可修改
元组中元素的元素可以修改
如:
>>> tp=(1,'a',['x'])
>>> tp[2].append('y')
>>> print(tp)
(1, 'a', ['x', 'y'])
| __add__(self, value, /)| Return self+value.>>> tp=(1,2,3)>>> tp.__add__(('a','b','c'))
(1, 2, 3, 'a', 'b', 'c')| __len__(self, /)|Return len(self).|count(...)| T.count(value) -> integer -- returnnumber of occurrences of value|index(...)| T.index(value, [start, [stop]]) -> integer -- returnfirst index of value.| Raises ValueError if the value is not present.
help(tuple)
提示:创建元组或列表后面最好带逗号 如 ls=[1,2,3,] 、tp=(1,2,3,)
python序列数据类型_Python 数据类型 之 序列类型相关推荐
- python dict下标_python数据类型性能问题
Python中变量的类型 了解过python语言的同学都知道,Python中的变量不需要声明,变量是在我们給它赋值的时候创建的,要使用一个变量必须先对它进行赋值. 在Python中变量就是变量,它没有 ...
- python数据类型_Python数据类型
python数据类型 Python Data Types are used to define the type of a variable. Previously we learned about ...
- python datatype函数_python 数据类型 datatype
python 数据类型 datatype 列表list 元组tuple 集合set 字典dictionary 字符串string 一.列表list list :python的一种数据类型.是可变的,有 ...
- python数据整理_python数据类型整理
Python中常见的数据结构可以统称为容器(container).序列(如列表和元组).映射(如字典)以及集合(set)是三类主要的容器. 一.序列(列表.元组和字符串) 序列中的每个元素都有自己的编 ...
- 写出python中6种数据类型_Python数据类型(一)
一.在Python中,基本数据类型主要可分为以下几种: 数字(Number): 字符串(String): 列表(List): 字典(Dictionary): 元组(Tuple): 1.在Python3 ...
- python序列数据类型_python 数据类型 序列——列表
python 数据类型 序列--列表 浏览次数: 发布时间:2015-08-21 11:38 python 数据类型 序列--列表 **列表** list是处理一组有序项目的数据结构,即你可以在一个列 ...
- python查看dataframe数据类型_python pandas中DataFrame类型数据操作函数的方法
python数据分析工具pandas中DataFrame和Series作为主要的数据结构. 本文主要是介绍如何对DataFrame数据进行操作并结合一个实例测试操作函数. 1)查看DataFrame数 ...
- python中加减乘除是什么数据类型_python中,数字类型计算
说明: 今天在看python数字类型的操作,在此记录下. 操作过程: 1.数字的加减乘除 >>> 2 + 2 4 >>> 4 - 2 2 >>> ...
- python中包含的标准数据类型_Python数据类型基础
1. Python标准数据类型 Python3 中有六个 标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Set(集合) Dictionary(字 ...
最新文章
- “鸟枪换炮”,nanopore测序在宏基因组中的应用
- python自动退出程序_python异常退出
- .net语言_Excel处理控件Aspose.Cells v20.3 Java.NET语言开启3月新版功能
- ThinkPHP框架 _ 学习3
- Hadoop中的Streaming(20)
- 物联网项目开发工作笔记0001---物联网项目的开发周期,项目管理,厂家合作
- EntityFramework 如何查看执行的 SQL 代码?
- 红帽 linux 安装gns3,Linux下安装GNS3
- 用友nccloud 虚拟机
- mapgis6.7原创2019视频教程
- CWMP协议(TR069协议)学习
- 一个互联网研发团队的标准配置
- win10将用户文件夹改为英文
- githubpage 配置 出现DNS解析失败
- 沉淀+树脂吸附工艺为铅酸电池废水处理提供新思路
- Elasticsearch 快速检索的秘诀
- 微信小程序如何做触底加载分页功能
- (CRON) info (No MTA installed, discarding output)” error in the syslog
- PHP通过CURL爬取飞猪国际机票
- js处理移动端有虚拟按键影响页面布局的处理方法
热门文章
- Shell常用命令总结
- Android面试题总结加强版(二)
- sql LEFT JOIN RIGHT JOIN(左连接)(mysql)
- 机器学习模型 知乎_机器学习-模型选择与评价
- class没有发布到tomcat_总在说SpringBoot内置了tomcat启动,那它的原理你说的清楚吗?
- 全景图解高铁数据,谁是最有潜力的高铁城市?
- oracle中sum和count可以嵌套吗_【分享吧】Oracle查询转换
- linux sed 空间模式,整理:SED的模式空间与缓冲区及n,N,d,D,p,P,h,H,g,G,x解析...
- require引入js vue_请教 关于使用require 引入vue 和公共js的问题
- python设置字符间距_python字符串处理以及字符串格式化