- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
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的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
第一段代码工作正常,并给出了我需要的结果。我现在想做的是让它在 'as num' 上返回 3 个数字值对于“as num”上的 3 个不同值,对于同一列上的 3 个不同位置 SELEC
我想分析一些数据以编写定价算法。以下日期可用: 我需要三个变量/维度的函数/相关因子,它显示三个维度(pers_capacity、卧室数量、浴室数量)增长时中位数(价格)的变化。例如Y(#pers_c
正如标题所说 - 我的 Sprite Kit 游戏时不时地在后台崩溃,总是出现此错误 - Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Sub
假设我尝试保存以下数据,并且Songs模型的name属性上设置了Phalcon \ Mvc \ Model \ Validator \ PresenceOf验证器 // Get an existing
我有一个 if 控件,如下所示; if (Directory.Exists(System.IO.Path.Combine(systemPath, "Reports", companyName))
有人可以告诉我我们使用 ReadLine() 从文件 (.txt) 中读取特定行吗?现在我想读取文件的全部内容(不仅仅是第一行)。为此我需要使用什么方法。我用谷歌搜索了很多,但找不到解决方案。 我的代
我相信在大学时我用从 C 派生的语言为 FPGA 编写了一个程序。我了解 VHDL 和 verilog 等语言。但是,我不明白的是程序员在使用哪个方面有多少选择?它依赖于FPGA吗?我将使用 Xili
我有一个 if 控件,如下所示; if (Directory.Exists(System.IO.Path.Combine(systemPath, "Reports", companyName))
如何在运行时更改 Dashcode (Javascript) 中图像对象的源? 我试过: var image = document.getElementById("image").object;ima
我有几个相互关联的类,它们将被多种不同的算法使用 例子: struct B; struct A { B* parent; }; struct B { std::vector child
我正在开发一个网站,用户在客户收到的表中输入金额,如果任何客户没有提供分期付款(金额),则用户不会在表中输入任何金额,并且用户希望获取违约者的信息客户以10天为基础。所以我的问题是应该定义什么表和属性
我试图从上一个条目中选择一个值,并每次将该数字加一。我让它工作到选择当前条目值(默认 1000)并递增 1 并重新插入该值(因此每次最终都是 1001)。我需要它来选择该字段的最后一个条目,这样它将变
我不擅长“制作”查询。假设这是我的数据库: artist pics ------------------- -
最近,我要为我的网站做一个即时通知系统。我听说 COMET 在这种情况下必不可少。 我已经搜索 PHP 和 Comet 一段时间了,但是,我发现的指南和文章似乎只是循环中的 ajax 请求。例如,有一
我正在开发一款 iOS 游戏,我希望 clown 在场景外生成,然后向下移动。我的想法是全部创建它们,并将它们以 360 像素的距离放置在不可见的场景中。 像这样: SKSpriteNode *clo
我有以下子订单表。 mysql> select * from suborder; +-------------+------------------+ | order_state | bookin
这可能是一个有点初学者的问题,但考虑到在 Java 中调试编码是相当相关的:什么时候编码与 String 对象相关? 假设我有一个要保存到文件中的字符串对象。 String 对象本身是否使用某种我应该
首先我想说我是 CPP 的新手(我从 cpp11 开始):)考虑以下实体:学生(名字+姓氏)和组(描述+更多学生)。我在 C++ 中创建了以下 2 个类: class Student { privat
我正在尝试在单击该复选框时同步更新我的数据库。我决定使用 aJax,但它似乎无法识别 ajax。 代码:将成为 Switch_Active(this.id) 函数的元素 ... Deactivat
我正在创建一个菜单。菜单如下。 $('.category').mouseover(function() { $(this).removeClass('category').addClass('cate
我是一名优秀的程序员,十分优秀!