python中什么是序列,列表,元组,字符串,索引,区别是什么?

Python024

python中什么是序列,列表,元组,字符串,索引,区别是什么?,第1张

python中什么是序列,列表,元组,字符串,索引,区别是什么?

序列:可通过偏移量来进行切片的对象。列表、元组、字符串都属于序列。

散列:无法通过偏移量来进行切片的对象。比如 *** 、字典

序列包括:列表、元组、字符串

列表:是最常用的数组,可以进行嵌套;

元组:相当于Enum,一旦创建,元组的值是不允许修改的;

字符串:顾名思义就是中英文字符以及标点符号等。

索引:序列中的每个元素被分配一个序号。注意索引默认从0开始。

python中列表,元组,字符串如何互相转换

python中有三个内建函数:列表,元组和字符串,他们之间的互相转换使用三个函数,str(),tuple()和list(),具体示例如下所示:>>>s = "xxxxx"

>>>list(s)

['x', 'x', 'x', 'x', 'x']

>>>tuple(s)

('x', 'x', 'x', 'x', 'x')

>>>tuple(list(s))

('x', 'x', 'x', 'x', 'x')

>>>list(tuple(s))

['x', 'x', 'x', 'x', 'x'] 列表和元组转换为字符串则必须依靠join函数

1. str转list

list = list(str)

2. list转str

str= ''.join(list)

3. tuple list相互转换

tuple=tuple(list)

list=list(tuple)

python中字符串方法isnumeric和isdigit的区别是什么

isdigit()

True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字

False: 汉字数字

Error: 无

isnumeric()

True: Unicode数字,全角数字(双字节),罗马数字,汉字数字

False: 无

Error: byte数字(单字节)

本文实例讲述了 Python中列表元素转为数字的方法 。

有一个数字字符的列表:

numbers_list = ['1', '3', '9', '5']

想要把每个元素转换为数字:

numbers_list = ['1', '3', '9', '5']

用一个循环来解决:

new_numbers_list = []

for n in numbers_list :

  new_numbers_list .append(int(n))

numbers_list = new_numbers_list 

使用列表推导式

numbers_list = [ int(x) for x in numbers_list ]

python2.x使用map语句

numbers_list = map(int, numbers_list )

python3.x使用map语句

numbers_list = list(map(int, numbers_list ))

复杂点

for i, v in enumerate(numbers_list ): 

    numbers_list [i] = int(v)