容器:列表、元组、字典

索引  index

  作用:定位单个容器元素。

  语法:容器[整数]

  说明:

    正向索引从0开始,第二个索引为1,最后一个为len(s)-1

    反向索引从-1开始,-1代表最后一个,-2代表倒数第二个,以此类推,第一个是-len(s)

切片  slice

  作用:定位多个容器元素

  语法:[开始索引,结束索引,步长]

  说明

    结束索引不包含该位置元素

    步长是切片每次获取完当前元素后移动的偏移量

    开始、结束和步长都可以省略

练习

#    字符串: content = "我是京师监狱狱长金海。"
#    打印第一个字符、打印最后一个字符、打印中间字符
#    打印字前三个符、打印后三个字符
#    命题:金海在字符串content中
#    命题:京师监狱不在字符串content中
#    通过切片打印“京师监狱狱长”
#    通过切片打印“长狱狱监师京”
#    通过切片打印“我师狱海”
#    倒序打印字符
content = "我是京师监狱狱长金海。"
center = len(content)
a = center//2
print(content[0])
print(content[-1])
print(content[a])  # 0.1.2.3.4.5.6.7.8.9.10.11
print(content[:3])
print(content[:-4:-1])
print("金海" in content)
print("京师监狱" not in content)
print(content[2:-3])
print(content[-4:1:-1]) #
print(content[0:-1:3])
print(content[::-1])

列表 list

1.创建

  语法1:列表名 = [数据1,数据2,数据3]

  语法2:列表名 = list(可迭代对象)

2.添加

  语法1:追加     列表名.append(数据)  在列表末尾加入数据

  语法2:插入     列表名.insert(索引,数据)  在指定索引前插入数据

3.定位

  语法1:索引     列表名[整数]

    --读取

    item = 列表名[ 索引]

    --修改

    列表名[索引] = "元素"

  语法2:切片    

    --读取 : 创建了新列表(拷贝)

    

list_new = list_old[:2]

    --修改 : 遍历右侧可迭代对象,依次存入左侧定位的区域

list_old[:2] = ["插入元素1","插入元素2"]

4.遍历

  1.从头到尾读取元素for 变量名 in 列表名:

    

for i in list

  2.非从头到尾读取元素

for i in range(len(list)):
    list[i] = ""
for i in range(len(list)-1,-1,-1)
    print(list[i])

5.删除

  语法1:列表名.remove(数据)

  语法2:del 列表名[索引]    del 列表名[切片]

列表转换为字符串

  result = "连接符".join(列表)

result = []
for number in range(10):
    result.append(str(number))
result = "".join(result)
print(result)  # 0123456789

字符串转换为列表

  列表 = "a-b-c-d".split("分隔符")

line = "悟空-唐三藏-八戒"
list_names = line.split("-")
print(list_names)   # ['悟空', '唐三藏', '八戒']

练习

1.    在终端中,循环录入字符串,如果录入空则停止.
       停止录入后打印所有内容(一个字符串)

list_result = []
while True:
    content = input("请输入内容:")  # 我爱python
    if content == "":
        break
    list_result.append(content)

str_result = "_".join(list_result)
print(str_result)                  # 我爱python

2.  将下列英文语句按照单词进行翻转.
      转换前:To have a government that is of people by people for people
      转换后:people for people by people of is that government a have To

message = "To have a government that is of people by people for people"
# 使用空格拆分字符串为列表
list_temp = message.split(" ")
# 使用切片翻转列表,再用空格连接
message = " ".join(list_temp[::-1])
print(message)

列表推导式

  列表 = [对变量操作 for 变量 in 可迭代对象 if 对变量的判断]

  适用性: 根据可迭代对象构建列表

需求1:将list01中大于10的数字,存入另外一个空列表中

list01 = [4, 54, 56, 67, 8]
# list_result = []
# for item in list01:
#     if item > 10:
#         list_result.append(item)
list_result = [item for item in list01 if item > 10]
print(list_result)


需求2:创建列表存储1-10之间数字的立方

# list_result = []
# for number in range(1,11):
#     list_result.append(number ** 3)
list_result = [number ** 3 for number in range(1, 11)]
print(list_result)

元组 tuple

    元组tuple 基础操作
创建
写法1:元组名 = (数据1, 数据2, 数据3)
tuple_name = ("邱乾清", "刘斯博", "夏锡亮")
写法2:元组名 = (数据1, 数据2, 数据3)
可变对象(善于改变) --> 不可变对象(节省内存)

list_data = [10, 20, 30]
tuple_result = tuple(list_data)

定位
-- 索引

print(tuple_name[-1])

-- 切片: 创建新元组

print(tuple_name[:2])

遍历
从头到为 读取 元素
# 快捷键:iter + 回车

for item in tuple_name:
    print(item)

非从头到为 读取 元素

for i in range(len(tuple_name) - 1, -1, -1):
    print(tuple_name[i])

注意1:如果元组中只有一个元素,必须加上逗号

tuple01 = ("数据",)
print(type(tuple01))

注意2:构建元组的括号可以省略

tuple02 = "数据1", "数据2", "数据3"
print(tuple02)

注意3:序列拆包

a, b, c = (10, 20, 30)
a, b, c = [10, 20, 30]
a, b, c = "孙悟空"
print(a)
print(b)
print(c)

字典 dict

1. 创建
语法1:字典名 = {键1: 值1, 键2: 值2}

dict_lsw = {"name": "李世伟", "age": 26, "sex": ""}
dict_yjm = {"name": "杨建蒙", "age": 23, "sex": ""}

语法2:字典名 = dict(可迭代对象)
注意:可迭代对象内的元素必须能够一分为二
   [(  ,  ) , (  ,  )]

list01 = ["唐僧", ("", "八戒"), ["", ""]]
dict01 = dict(list01)
print(dict01)

2. 添加(键不存在)   字典名[键] = 值

if "money" not in dict_lsw:
    dict_lsw["money"] = 10000000

3. 定位
键  字典名[键]
-- 读取

value = dict_lsw["age"]
print(value)
-- 修改(键存在)
if "age" in dict_lsw:
    dict_lsw["age"] = 38
dict_lsw = {"name": "李世伟", "age": 26, "sex": ""}

1. 遍历
语法1:for 键 in 字典名:

for key in dict_lsw:
    print(key)

语法2:for 值 in 字典名.values():

for value in dict_lsw.values():
    print(value)

语法3:for 键,值 in 字典名.items():

for key, value in dict_lsw.items():
    print(key)
    print(value)

如果需要通过索引或者切片操作字典数据

那么就要转换为序列(元组,列表)

tuple_keys = tuple(dict_lsw)
tuple_values = tuple(dict_lsw.values())
tuple_k_v = tuple(dict_lsw.items())
print(tuple_k_v)

删除  del 字典名[键]

if "sex" in dict_lsw:
    del dict_lsw["sex"]

print(dict_lsw)

字典推导式

  字典名 = {键的操作:值的操作 for 变量 in 可迭代对象 if 条件}

需求: key:1-10   value:key的平方

# dict_result = {}
# for number in range(1, 11):
#     dict_result[number] = number ** 2
# print(dict_result)
dict_result = {number: number ** 2 for number in range(1, 11)}

需求:将dict_result中偶数键存入另外一个字典

# dict_new = {}
# for key, value in dict_result.items():
#     if key % 2 == 0:
#         dict_new[key] = value
# print(dict_new)
dict_new = {key: value for key, value in dict_result.items() if key % 2 == 0}

集合 set 基础操作

    作用1:去重复

创建
语法1:集合名 = {数据1,数据2,数据3}

set_name = {"杨建蒙", "衣俊晓", "杨建蒙", "常磊", "杨建蒙"}

语法2:集合名 = {数据1,数据2,数据3}

list01 = [10, 20, 10, 10, 30]
set01 = set(list01)
print(set01)

添加

set_name.add("邱乾清")
set_name.add("吴宗净")

遍历

for item in set_name:
    print(item)

删除

if "吴宗净" in set_name:
    set_name.remove("吴宗净")

数学操作

1. 交集&:返回共同元素。

s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = s1 & s2  # {2, 3}

2.并集:返回不重复元素

s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = s1 | s2  # {1, 2, 3, 4}

3.补集-:返回只属于其中之一的元素

s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = s1 - s2  # {1} 属于s1但不属于s2

补集^:返回不同的的元素

s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = s1 ^ s2  # {1, 4}  等同于(s1-s2 | s2-s1)

4.子集<    超集>

s1 = {1, 2, 3}
s2 = {2, 3}
print(s2 < s1)  # True
print(s1 > s2)  # True
Live what we love, do what we do, follow the heart, and do not hesitate.
原文地址:https://www.cnblogs.com/failan/p/13662314.html