什么是序列:有序的一列,如列表、元组、字符串
序列的操作:索引、切片、相加、相乘、成员资格检查
索引:序列中的所有元素位置都有编号,从0开始递增;eg: string = '你好,世界',string[2]=,string[-1]=界
切片:选取序列的一部分 string[5::-1]表示从头开始,步长为-1
相加:相当于拼接序列
相乘:>>> "a"*5
'aaaaa'
>>> ["a"]*5
['a', 'a', 'a', 'a', 'a']
>>> ("a",)*5
('a', 'a', 'a', 'a', 'a')
成员资格:检查成员
>>> "H" in "hollo"
False
序列解包和序列封包:
简单来说就是将序列的值赋值给一个变量,将多个变量值赋值一个序列。:
In [82]: x,y,z = 1,2,3
In [83]: x,y = y,x
In [84]: print(x,y,z)
2 1 3
定义:形如list = [1,22,3]构成的一个序列
列表的操作:
# 所有的操作都针对list1本身进行了修改
list1 = [1, 3, 5, 7, 9]
list1.append(8) # 列表内容增加,单个
list1.extend([2,1,3,9]) # 列表内容的增加,多个
list1.insert(1, 4) # 插入,位置和值
list1[-1] = 0 # 正宗的列表修改
print(list1)
del list1[-2]
print(list1.pop(2)) # 列表删除元素,并且返回,元素值--栈
print(list1)
list1.remove(1) #删除第一个指定值的元素
print(list1)
print(list1.index(5)) #列表的索引,相当于查找 返回值的位置
print(list1.count(6)) # 列表内容的查找,返回值的出现次数
list1.reverse() # 列表翻转
print(list1)
list1.sort() # 列表排序,有大到小
print(list1)
list1.sort(reverse=True)
print(list1)
# list1.sorted() ????
list2 = list1.copy() # 复制
print(list2)
list1.clear() #清空列表
print(list1)
定义:tuple(1,3,5,7,9)
特性:1.元组是不可修改的;2.逗号分隔(1,)
其余与列表类似
定义:str1 = "abcdefg"
操作:1.序列基本操作(切片、索引、乘法、相加、成员资格检查、最值)
2.字符串格式设置:
1.精简版:使用格式设置运算符:str2 = "hello, %s"
values = "world!"
str2%values
"hello, world!"
2.完整版:.format方法
In [14]: "{0:-^20,.3f}".format(25.687925)
Out[14]: '-------25.688-------'
In [16]: "{0:-^20,.3e}".format(2545.687925)
Out[16]: '-----2.546e+03------'
从左至右:序号,引导符,填充符,对齐方式,槽长度,数字千位分隔,精度,浮点数表示方法
3.方法:
In [17]: str1 = "hello, world!"
In [19]: str1.center(20,"-")
Out[19]: '---hello, world!----'
In [20]: str1.find("l")
Out[20]: 2
In [21]: str1.find("g")
Out[21]: -1
In [22]: str1.find("l",3,10)
Out[22]: 3
In [23]: str1.find("l",4,10)
Out[23]: -1
In [29]: str1.title()
Out[29]: 'Hello, World!'
In [30]: str1.replace("world","python")
Out[30]: 'hello, python!'
In [31]: " hello, world! ".strip()
Out[31]: 'hello, world!'
In [32]: " hello, world!** ".strip("*")
Out[32]: ' hello, world!** '
In [33]: " hello, world!** ".strip("*!")
Out[33]: ' hello, world!** '
In [34]: " hello, world!****".strip("*!")
Out[34]: ' hello, world'
In [35]: " hello, world! ".lstrip()
Out[35]: 'hello, world! '
In [36]: " hello, world! ".rstrip()
Out[36]: ' hello, world!'
In [38]: trans = str.maketrans("l","L")
...: " hello, world!".translate(trans)
Out[38]: ' heLLo, worLd!'
is+方法用于判断
定义:类似于字典,反应的是一种映射关系,由键和值构成
dict1 = {"zhangsan":"18","lisi":"19","wangwu":"17"}
方法:
在这里插入代码片
clear,copy,items,fromkeys,keys,values,pop,setdefult,update
In [39]: dict1 = {"zhangsan":"18","lisi":"19","wangwu":"17"}
In [40]: len(dict1)
Out[40]: 3
In [44]: dict1["zhangsan"]
Out[44]: '18'
In [45]: dict1["zhangsan"] = "22" #修改值没有时添加
In [46]: dict1["zhangsan"]
Out[46]: '22'
In [47]: "liuqi" in dict1
Out[47]: False #判断成员
In [49]: dict1["liuqi"] = "21"
# clear清空字典项,如果一个字典赋值给另一个,也会清空
In [50]: x = {}
In [51]: y = x
In [52]: x["key"]="value"
In [53]: y
Out[53]: {'key': 'value'}
In [54]: x={}
In [55]: y
Out[55]: {'key': 'value'}
In [56]: x=y
In [57]: x
Out[57]: {'key': 'value'}
In [58]: x.clear()
#字典赋值,深赋值和浅赋值两种浅赋值用的是原来的键-值对
In [59]: y
Out[59]: {}
In [60]: dict1
Out[60]: {'zhangsan': '22', 'lisi': '19', 'wangwu': '17', 'liuqi': '21'}
In [61]: dict2 = dict1.copy()
In [62]: dict2["zhaojiu"]="24"
In [63]: dict1
Out[63]: {'zhangsan': '22', 'lisi': '19', 'wangwu': '17', 'liuqi': '21'}
In [64]: dict2
Out[64]:
{'zhangsan': '22',
'lisi': '19',
'wangwu': '17',
'liuqi': '21',
'zhaojiu': '24'}
In [67]: {}.fromkeys(["name","age","sex"]) #create a dict has no values
Out[67]: {'name': None, 'age': None, 'sex': None}
In [68]: print(dict1.get("sunshi"))
None
In [70]: dict1.items() #以列表的方式返回所有内容
Out[70]: dict_items([('zhangsan', '22'), ('lisi', '19'), ('wangwu', '17'), ('liuqi', '21')])
In [71]: dict1.keys()
Out[71]: dict_keys(['zhangsan', 'lisi', 'wangwu', 'liuqi'])
In [72]: dict1.pop("liuqi") # dilete the key and return the value
Out[72]: '21'
In [73]: dict1.setdefault("sunshi","25")
Out[73]: '25'
In [74]: dict1
Out[74]: {'zhangsan': '22', 'lisi': '19', 'wangwu': '17', 'sunshi': '25'}
In [75]: x = {"sunshi","15"}
In [76]: dict1
Out[76]: {'zhangsan': '22', 'lisi': '19', 'wangwu': '17', 'sunshi': '25'}
In [78]: x = {"sunshi":"15"}
In [79]: dict1.update(x) # use a dict to update the dict
In [80]: dict1
Out[80]: {'zhangsan': '22', 'lisi': '19', 'wangwu': '17', 'sunshi': '15'}
定义:无序的,可以为空,可以装任意类型数据(不能字典),不能重复
set{"abcdefghijk"}
集合的操作:交并差补
In [86]: set1 = {1,2,3,4,5,7,9,0}
In [87]: set2 = {4,6,8,10,0}
In [88]: set1&set2
Out[88]: {0, 4}
In [89]: set1.intersection(set2)
Out[89]: {0, 4}
In [90]: set1.union(set2)
Out[90]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
In [91]: set1|set2
Out[91]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
In [92]: set1-set2
Out[92]: {1, 2, 3, 5, 7, 9}
In [93]: set2-set1
Out[93]: {6, 8, 10}
In [94]: set1.difference(set2)
Out[94]: {1, 2, 3, 5, 7, 9}
In [95]: set1^set2
Out[95]: {1, 2, 3, 5, 6, 7, 8, 9, 10}
# 子集和父集(<=,>=)
In [96]: set1.issubset(set2)
Out[96]: False
In [97]: set1.issuperset(set2)
Out[97]: False
```