#12.25学习记录
>>> import io
>>> s = "hello,sxt"
>>> sio = io.StringIO(s)
>>> sio
<_io.StringIO object at 0x0000000002EE3DC8>
>>> d = "hello,sxt"
>>> dio = io.StringIO(d)
>>> dio
<_io.StringIO object at 0x0000000002EB9438>
>>> dio.getvalue()
'hello,sxt'
>>> dio.seek(1)
1
>>> dio.write("b")
1
>>> dio.getvalue()
'hbllo,sxt'
>>>
以下分别为直接生成, list() 生成 range()生成 推导式生成列表
>>> a = [20,30,40,"hangye"]
>>> a=list()
>>> a
[]
>>> a=list("hangye")
>>> a
['h', 'a', 'n', 'g', 'y', 'e']
>>> range(10)
range(0, 10)
>>> print(range(10))
range(0, 10)
>>> a=range(10)
>>> type(a)
<class 'range'>
>>> list(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(0,10,1))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(5,20,2))
[5, 7, 9, 11, 13, 15, 17, 19]
>>> list(range(5,20,-1))
[]
>>> list(range(20,5,-1))
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6]
>>> list(range(20,5,-1))#从右向左数
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6]
>>> a=[x*2 for x in range(5)]
>>> a
[0, 2, 4, 6, 8]
>>> a=[x*2 for x in range(100)if x%9==0]
>>> a
[0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180, 198]
>>>
一般只对列表尾部添加或删除元素
append()
a=[20,40]
a.append(80)
a
[20, 40, 80]
a=[20,40]
id(a)
49388872
+符号
a=[20,40]
id(a)
49388872
a=a+[30,50]
a
[20, 40, 30, 50]
id(a)
49584136
id(a)#经过加法后产生新对象,
49584136
extend() 在原有对象基础上进行增加
a=[20,40]
id(a)
49331720
a.extend([50,60])
id(a)
49331720
insert() 插入后 让其它数据往后挪动,造成大量的元素移动 remove() pop() del() 等都要避免这些
a = [10,20,30,40]
a.insert(1,15)
a
[10, 15, 20, 30, 40]
乘法扩展
>>> a
[10, 15, 20, 30, 40]
>>> b=a*3
>>> b
[10, 15, 20, 30, 40, 10, 15, 20, 30, 40, 10, 15, 20, 30, 40]
>>>
del 整个过程其实就是数组的拷贝 类似插入的操作
>>> A=[100,200,888,300,400]
>>> del A[2]
>>> A
[100, 200, 300, 400]
>>>
pop() 删除并返回制定位置元素 下列代码 不断对a进行操作 也可指定位置pop(1)
>>> a=[100,200,888,300,400]
>>> b = a.pop()
>>> b
400
>>> c = b.pop()
Traceback (most recent call last):
File "" , line 1, in <module>
c = b.pop()
AttributeError: 'int' object has no attribute 'pop'
>>> c = a.pop()
>>> c
300
>>> a
[100, 200, 888]
>>>
remove() 直接操作元素 删除首次出现的元素 若不存在则剖出异常
>>> a=["a","b","c","d","e"]
>>> a.remove("c")
>>> a
['a', 'b', 'd', 'e']
>>> a.remove("f")
Traceback (most recent call last):
File "" , line 1, in <module>
a.remove("f")
ValueError: list.remove(x): x not in list
>>>
索引直接访问元素,超范围则报错
index() 获得指定元素首次出现的索引
count() 获得指定元素首次出现的次数
len() 获得列表长度
成员资格判断 一般用in 或者 not in来判断 也可以用count()来判断
>>> a = [10,20,30,40,50,20,30,20,30]
>>> a.index(20)
1
>>> a.index(30,3)
6
>>> "a" in "abc"
True
>>> 10 in [10,20,30,10]
True
>>> 10 in [100,200,300]
False
>>> a = [100,200,300,100]
>>> a.count(100)
2
>>> a.count(100)>0
True
>>> len(a)
4
>>>
>>> a = [10,20,30,40,50,60]
>>> a[:]
[10, 20, 30, 40, 50, 60]
>>> a[1:5:2]
[20, 40]
>>> a[1:5:6]
[20]
>>> a[1:6:2]
[20, 40, 60]
>>> a[0:6:2]
[10, 30, 50]
>>> a[1:]
[20, 30, 40, 50, 60]
>>> a[:3]
[10, 20, 30]
>>> a[-5;-3]
SyntaxError: invalid syntax
>>> a[-5:-3]
[20, 30]
>>> a[:-1]
[10, 20, 30, 40, 50]
>>> a[::-1]
[60, 50, 40, 30, 20, 10]
>>> a[0:6:2]#[起始偏移量:终止偏移量-1:步长]
[10, 30, 50]
列表的遍历
>>> for b in a:
print(b)
10
20
30
40
50
60
>>>
升序,降序,返回逆序排列
>>> #修改原列表,不件新列表的排序
>>> a = [20,10,30,40]
>>> id(a)
47702664
>>> a.sort()
>>> a
[10, 20, 30, 40]
>>> # 默认按照升序排列
>>> id(a)
47702664
>>> a.sort(reverse=True) #降序排列
>>> a
[40, 30, 20, 10]
>>> import random #调用随机模块
>>> random.shuffle(a)
>>> a
[20, 30, 40, 10]
>>> #创建新列表的排序
>>> a = [20,10,30,40]
>>> id(a)
49388872
>>> a = sorted(a)
>>> a
[10, 20, 30, 40]
>>> id(a)
49434824
>>> #reversed() 返回迭代器
>>> c = reversed(a)
>>> c
<list_reverseiterator object at 0x00000000028E1708>
>>> list(c)
[40, 30, 20, 10]
>>> list(c)#迭代器只能用一次,后边就不能用了
[]
>>>
max() min() 返回最大最小值
sum() 对数值型列表元素求和,但不适用于其它类型列表
a = [
["gaoyi",18,300,"beijing"],
["gaoer",18,300,"tianjing"],
["gaosi",20,300,"beijing"]
]
for m in range(3): #一维列表为3个
for n in range(4): #二维列表是4个
print(a[m][n],end="\t")
print() #打印完一行,换行
多维列表开始循环
>>> a=[]
>>> a = [
["gaoyi",18,300,"beijing"],
["gaoer",18,300,"tianjing"],
["gaosi",20,300,"beijing"]
]
>>> a
[['gaoyi', 18, 300, 'beijing'], ['gaoer', 18, 300, 'tianjing'], ['gaosi', 20, 300, 'beijing']]
>>> a[0]
['gaoyi', 18, 300, 'beijing']
>>> a[0][0]
'gaoyi'
元组里边,不可变序列,不能修改元组中的元素,只有元组创建和删除,需要了解元素的访问和计数
索引访问 切片操作 连接操作 成员关系操作 比较运算操作 计数:len() max() min() sum()
1 小括号创建 也可省略小括号 仅有一个数要带逗号
>>> a = (10,20,30)
>>> type(a)
<class 'tuple'>
>>> b=(10)
>>> type(b)
<class 'int'>
>>> b=(10,)
>>> type(b)
<class 'tuple'>
>>>
>>> c = tuple()
>>> c
()
>>> b=tuple("abcd")
>>> b
('a', 'b', 'c', 'd')
>>> b=list("abcd")
>>> list
<class 'list'>
>>> b=tuple(range(3))
>>> b
(0, 1, 2)
>>> b=tuple([10,20,30])
>>> b
(10, 20, 30)
>>>
元组删除
>>> del b
>>> b
Traceback (most recent call last):
File "" , line 1, in <module>
b
NameError: name 'b' is not defined
>>>
访问同列表的命令
列表排序,用
>>> b=(30,20,40)
>>> sorted(b)
[20, 30, 40]
>>> #生成新的列表对象
>>> a=[10,20,30]
>>> b=[40,50,60]
>>> c=[70,80,90]
>>> d=zip(a,b,c)
>>> list(d)
[(10, 40, 70), (20, 50, 80), (30, 60, 90)]
>>>
>>> s = (x*2 for x in range(5))
>>> s
<generator object <genexpr> at 0x0000000002EBDAC8>
>>> tuple(s)#奖s变成一个元组
(0, 2, 4, 6, 8)
>>> tuple(s) #再生成时指针已经前移,生成器只用了一次
()
>>> s = (x*2 for x in range(5))
>>> s.__next__()
0
>>> s.__next__()
2
>>> s.__next__()
4
>>>
>>> s.__next__()
6
>>> s.__next__()
8
>>> s.__next__()
Traceback (most recent call last):
File "" , line 1, in <module>
s.__next__()
StopIteration
>>> s = (x*2 for x in range(5))
>>> s.__next__()#依次取用s中的元素
0
>>>