gpt4 book ai didi

Python3中的列表,元组,字典,字符串相关知识小结

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Python3中的列表,元组,字典,字符串相关知识小结由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

1、知识概要 。

  1. 列表,元组,字典,字符串的创建方式 。

  2. 列表,元组,字典,字符串的方法调用 。

  3. 列表,元组,字典,字符串的常规用法 。

2、列表 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 列 表
 
# 列表基础
list_1 = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ]
list_2 = [ 'apple' , 'banana' , 'watermelon' , 'strawberry' , 'banana' , 'apple' ]
print (list_1)
print ( "##########" )
# 列表得下标是从0开始的,之后的一次+1
print (list_1[ 0 ])
print ( "##########" )
print (list_1[ 4 ])
print ( "##########" )
print (list_1[ 1 : 3 ]) # 从下标为1的元素开始,到下标为3的终止
print ( "##########" )
print (list_1[ 1 : - 2 ])   # -2是指将列表的顺序倒置,结尾变为开头,下标相对来说变为0向前依次 -1
print ( "##########" )
print (list_1[:: 2 ]) # 前面两个冒号分别使用默认的参数,最后一个数字表示步长,两步一取
print ( "##########" )
 
# 列表的增、删、改
list_1.append( 'z' ) # 在结尾加一个元素
print (list_1)
print ( "##########" )
list_1.insert( 1 , 'y' # 在指定位置增加元素,在a后b前插入y
print (list_1)
print ( "##########" )
a = list_2.extend(list_1)    # 将list_1和list_2合并
print (a)
print ( "##########" )
list_1[ 4 ] = 'o'   # 修改第五个元素
print (list_1)
print ( "##########" )
list_1[ 2 : 3 ] = [ 'p' , 'q' ]   # 修改连续的元素
print (list_1)
print ( "##########" )
list_1.remove(list_1[ 3 ])    # 删除下标为3的元素
print (list_1)
print ( "##########" )
list_1.pop( 2 )    # 直接加下标
print (list_1)
print ( "##########" )
# del list_1[2]  删除列表中的下标为2的元素
# del list_1   直接删除掉列表
 
# 列表的一些方法
# count
print (list_2.count( 'apple' ))  # count是计算出现次数的方法
print ( "##########" )
 
# index
print (list_2.index( 'banana' ))    # 寻找banana在哪个位置
print ( "##########" )
one_apple = list_2.index( 'apple' )
print (one_apple)
list_3 = list_2[one_apple + 1 :]
two_apple = list_3.index( 'apple' )
list_4_index = one_apple + two_apple + 1
print (list_4_index)     # 输出第二个位置
print ( "##########" )
 
# reverse
list_1.reverse()  # 倒叙
print (list_1)
print ( "##########" )
 
# sort
list_5 = [ 6 , 4 , 3 , 7 , 5 , 5 , 8 , 1 ]
list_5.sort()    # 排序(从小到大)
print (list_5)
print ( "##########" )
 
# clear
list_5.clear()   # 清空列表
print (list_5)
print ( "##########" )

3、元组 。

?
1
2
3
4
# 元 组
  tuple1 = ( 1 ,)    # 只有一个元素的话,后面加一个逗号,对之后的学习有所帮助
  tuple2 = ( 1 , 2 , 3 , 4 , 5 )
  # tuple2[2] = 10  # 元组是不可修改的

4、字典 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 字 典
 
dictionary1 = { 'country' : 'China' , 'city' : 'beijing' # 创建字典(常用),前面为键,后面为键值
dictionary2 = dict ((( 'city' , 'shanghai' ),))   # 创建字典
# 键是不可变类型:整型,字符串,元组
# 可变类型有:列表,字典
print (dictionary1)
print (dictionary1[ 'city' ])
print (dictionary2)
# 字典两大特点:无序,键值唯一
print ( "##########" )
 
 
dictionary2[ 'city' ] = 'tianjin'   # 修改字典,增加内容
print (dictionary2)
print ( "##########" )
return1 = dictionary2.setdefault( 'location' , 'north' )   # 增加内容,如果有则不做修改
print (dictionary2)
print (return1)   # 返回键值
print ( "##########" )
print (dictionary1.keys())    # 查看字典当中用那些键
print ( list (dictionary1.keys()))   # 用列表的形式展示
print (dictionary1.values())   # 只查看键值
print (dictionary1.items())   # 将字典当中的所有键值对拿出
print ( "##########" )
 
dictionary3 = { 1 : 1 , 2 : 2 }
dictionary4 = { 1 : 3 , 4 : 5 , 6 : 7 }
dictionary3.update(dictionary4)   # 更新字典,如果有键相同,则更新键值;如完全没有,则更新在后方
print (dictionary3)
print (dictionary4)
print ( "##########" )
 
# 删除字典
eg = dictionary3.popitem()   # 不加键,则会随机删除键值对
print (eg, '||' ,dictionary3)
dictionary4.pop( 6 )   # 删除键为6的信息
print (dictionary4)
del dictionary4[ 4 ]   # 删除键为4的信息
print (dictionary4)
dictionary4.clear()   # 清空字典,只留下框架轮廓
print (dictionary4)
print ( "##########" )
 
dictionary5 = dict .fromkeys([ 'a' , 'b' , 'c' ],[ 'z' , 'y' ])   # 分配率,将后面最为一个整体
print (dictionary5)
 
dictionary5[ 'b' ][ 0 ] = 'x'  # 需要理解深浅拷贝
print (dictionary5)
print ( "##########" )
print ( sorted (dictionary5))   # 字典的排序
print ( sorted (dictionary5.values()))   # 根据值排序
print ( "##########" )
 
# 字典的遍历
dictionary6 = { 'num1' : 10 , 'num2' : 52 , 'num3' : 33 }
# 效率较高
for i in dictionary6:
   print (i,dictionary6[i])
print ( "##########" )
# 效率很低
for a,b in dictionary6.items():
   print (a,b)
print ( "##########" )

5、字符串 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 字符串
str1 = '1'
str2 = "2"   # Python中单引号与双引号没什么区别
print (str1)
print (str2)
print (str2 * 200 )
str3 = 'asdqwezxc'
print (str3[ 2 :])
print ( 'as' in str3)   # 判断此字段是否在字符串之中
print ( 'aq' in str3)
print (str1 + str2)    # 字符串拼接(效率很低)
eg1 = '......' .join([str1,str2])  # 通过单引号的符号连接将要拼接的两个字符串
print (eg1)
print ( "##########" )
 
# 字符串内置方法
str4 = 'it is a bea\tutiful city 是 {name}\n'
print (str4.count( 's' ))   # 统计数目
print (str4.capitalize())    # 字符串首字母大写
print (str4.center( 100 , '-' ))  # 居中(距离和符号)
print (str4.endswith( 'ful' ))   # 以某个内容结尾
print (str4.startswith( 'it' ))  # 以某个内容开始
print (str4.expandtabs(tabsize = 20 ))   # 对\t的空格数改为20,默认为4
print (str4.find( 'b' ))    # 查找到的第一个元素,返回下标号
print ( "##########" )
# 格式化输出
print (str4. format (name = 'beijing' ))  # 将name改为具体的名字
print (str4.format_map({ 'name' : 'shanghai' }))
print (str4.index( 'b' ))   # 查找索引值,和fund()相似,但是index找不到会报错
print (str4.isalnum())    # 不常用,是否是数字,字母,中文
print (str4.isdecimal())   # 不常用,是否是十进制的数
print (str4.isdigit())    # 是否为数字
print (str4.isnumeric())   # 是否为数字
print (str4.isidentifier())   # 是否为非法变量
print ( "##########" )
print (str4.islower())    # 是否都是小写
print (str4.isupper())    # 是否都是大写
print (str4.isspace())    # 是否是个空格
print (str4.istitle())    # 每一个词的首字母是否是大写
print (str4.lower())     # 大写全部变小写
print (str4.upper())     # 小写全部变大写
print (str4.swapcase())   # 字母大小写反转
print (str4.ljust( 100 , ':' ))     # 向左对齐
print (str4.rjust( 100 , ':' ))     # 向右对齐
print ( "##########" )
print (str4.strip())     # 去掉换行符,空格
print ( 123 )     # 前面的换行符被去掉
print ( "##########" )
print (str4.replace( 'city' , '城市' ))    # 替换内容,也可以部分替换
print (str4.rfind( 't' ))   # 真实的索引位置
print (str4.split( ' ' ))   # 字符串的分割
print (str4.rsplit( 'b' , 1 ))  # 以右为准,以目标分割,分割一次

总结 。

以上就是本文关于Python3中的列表,元组,字典,字符串相关知识小结的全部内容,希望对大家有所帮助。有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持! 。

原文链接:http://www.cnblogs.com/Infi-chu/p/7337033.html 。

最后此篇关于Python3中的列表,元组,字典,字符串相关知识小结的文章就讲到这里了,如果你想了解更多关于Python3中的列表,元组,字典,字符串相关知识小结的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com