3.2 数据类型

列表

列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作。

定义列表

names = ['zhangsan','lisi','wangwu']

通过下标访问列表中的元素,下标从0开始计数

print(name[0])

运行结果为:

zhangsan

取多个元素

names[1:4:2]  #取下标1至下标4之间的数字,每隔一个元素,就取一个,包括1,不包括4

追加

names.append('xiaoliu')

插入

names.insert(1, "aa")

运行结果为:

['zhangsan', 'aa', 'lisi', 'wangwu', 'xiaoliu']

修改

names[1] = "bb"

删除

按下标删除

del names[1]

按指定元素删除

names.remove("zhangsan")

删除列表中最后一个值

names.pop()

扩展

names = ['zhangsan','lisi','wangwu']
ages = ['1','2']
names.extend(ages)

输出结果为:

['zhangsan', 'lisi', 'wangwu', '1', '2']

拷贝

name2 = names.copy()

统计

num = [1,2,3,4,2,3,4,2]
print(num.count(2))

输出结果为:

3

排序

num = [1,2,3,4,2,3,4,2]
num.sort()
print(num)

输出结果为:

[1, 2, 2, 2, 3, 3, 4, 4]

翻转

name.reverse()

获取下标

num = [1,2,3,4,2,3,4,2]
print(num.index(3))

输出结果为:

2

注意:只返回找到的第一个下标

元组

元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表

names = ('zhangsan','lisi','wangwu')
print(names, names.count("zhangsan"),names.index("lisi"))

输出结果为:

('zhangsan', 'lisi', 'wangwu') 1 1

字符串

特性:不可修改

首字母大写

name = "abc"
print(name.capitalize())

输出结果为:

Abc

大写全部变小写

name = "ABC"
print(name.casefold())

输出结果为:

abc

居中

name = "ABC"
print(name.center(50, "-"))

输出结果为:

-----------------------ABC------------------------

统计lex出现次数

name = "ABClexjjlllex"
print(name.count('lex'))

输出结果为:

2

将字符串编码成bytes格式

name.encode()

判断字符串是否已Li结尾

name = "ABClexjjlllexLi"
print(name.endswith("Li"))

输出结果为:

True

输出'Wangzhi Clay', 将\t转换成多长的空格

print("Wangzhi\tClay".expandtabs(20))

输出结果为:

Wangzhi Clay

find 查找A,找到返回其索引, 找不到返回-1

name = "BClexAjjlllex"
print(name.find('A'))

输出结果为:

5

format 格式化输出

msg = "my name is {}, and age is {}"
print(msg.format("clay", 18))

输出结果为:

my name is clay, and age is 18

msg = "my name is {1}, and age is {0}"
print(msg.format("clay", 18))

输出结果为:

my name is 18, and age is clay

msg = "my name is {name}, and age is {age}"
print(msg.format(age=18, name="clay"))

输出结果为:

my name is clay, and age is 18

format_map 格式化输出

msg = "my name is {name}, and age is {age}"
print(msg.format_map({'age':18, 'name':'clay'})

输出结果为:

my name is clay, and age is 18

index 返回索引

name = "BAClexAjjlllex"
print(name.index('A'))

输出结果为:

1

isalnum都是字母和数字

name = "k"
print(name.isalnum())

输出结果为:

True

isdigit 是否为整数

name = "5"
print(name.isdigit())

输出结果为:

True

isnumeric isprintable isspace isupper ????

join

print("|".join(['alex', 'jack', 'rain']))

输出结果为:

alex|jack|rain

maketrans 替换

intab = "aeiou"
outab = "12345"
trantab = str.maketrans(intab, outab)
str = "this is string example ... wow!!"
print(str.translate(trantab))

输出结果为:

th3s 3s str3ng 2x1mpl2 ... w4w!!

partition分隔

str = "this is string example ... wow!!"
print(str.partition('is'))

输出结果为:

('th', 'is', ' is string example ... wow!!')

swapcase 大小写互换

msg = "xiaoxieDAXIE"
print(msg.swapcase())

输出结果为:

XIAOXIEdaxie

zfill 用0补齐

msg = "xiaoxieDAXIE"
print(msg.zfill(20))

输出结果为:

00000000xiaoxieDAXIE

ljust 靠左侧补齐

msg = "xiaoxieDAXIE"
print(msg.ljust(20, "-"))

输出结果为:

xiaoxieDAXIE--------

rjust 靠右侧补齐

msg = "xiaoxieDAXIE"
print(msg.rjust(20, "-"))

输出结果为:

--------xiaoxieDAXIE

isidentifier 检测一段字符串可否被当作标志符,即是否符合变量命名规则

msg = "1_xiaoxieDAXIE"
print(msg.isidentifier())

False

Last updated