python学习之 list tuple range

版权声明:本文为博主原创文章,转载注明出处。 https://blog.csdn.net/pengjc2001/article/details/68944029
# Sequence Type --- list, tuple, range
# list 是一个可变序列(mutable sequence), 数据项可具有不同的类型,用','分割数据项,用[]括起来
# tuple 是一个不可变序列(inmutable sequence),元素不能修改,数据项可具有不同的类型,用','分割数据项,用()括起来
# range 是一个关于数字的不可变序列(inmutable sequence), 通常用来定义循环. range(stop) range(start,stop[,step])
# 创建
list0 = []
list1 = ['python','你好', 100, 3.14159]
tuple0 = ()
tupleOne = (2,)    #一个元素时注意要用逗号
tuple1 = ('python','你好', 100, 3.14159)
print ('list0:',list0,'\n','list1:',list1,'\n','tuple0:',tuple0,'\n','tupleOne:',tupleOne,'\n','tuple1:',tuple1)
print ('range(10):',list(range(10)))
print ('range(1,11):',list(range(1,11)))
print ('range(0,30,5):',list(range(0,30,5)))
#运行结果
list0: [] 
list1: ['python', '你好', 100, 3.14159] 
tuple0: () 
tupleOne: (2,) 
tuple1: ('python', '你好', 100, 3.14159)
range(10): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1,11): [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(0,30,5): [0, 5, 10, 15, 20, 25]

# Common Sequence Operations
# x in s   如果s中有元素等于x 返回 True, 否则 False
# x not in s 如果s中没有有元素等于x 返回 True, 否则 False
print ('python' in list1)
print ('0' not in list1)
print ('python' in tuple1)
print ('0' not in tuple1)
#运行结果
True
True
True
True

# s+t  将s、t组合在一起
print ("list1+list1 :",list1+list1)
print ("tuple1+tuple1 :",tuple1+tuple1)
# s*n or n*s 将自己组合n次
print ("list1*3 :",list1*3)
print ("tuple1*4 :",tuple1*4)
# s[i] 访问元素i, 初始元素是0, 如果从末尾开始访问,最后一个元素编号-1
print (list1[0],list1[-1])
print (tuple1[0],tuple1[-2])
#运行结果
list1+list1 : ['python', '你好', 100, 3.14159, 'python', '你好', 100, 3.14159]
tuple1+tuple1 : ('python', '你好', 100, 3.14159, 'python', '你好', 100, 3.14159)
list1*3 : ['python', '你好', 100, 3.14159, 'python', '你好', 100, 3.14159, 'python', '你好', 100, 3.14159]
tuple1*4 : ('python', '你好', 100, 3.14159, 'python', '你好', 100, 3.14159, 'python', '你好', 100, 3.14159, 'python', '你好', 100, 3.14159)
python 3.14159
python 100

# s[i:j] 切片 提取元素 i到j   注意 不包括 j  i<=k<j
# s[i:j] 切片 提取元素 i到j 步长k
listS = list(range(0,30))
print (listS)
print (listS[5],listS[9])
print (listS[5:9])
print (listS[0:25:5])
# len(s) s的长度
print (len(listS))
#min(s) s中最小值  此时 s中的数据是可比较的 同类型
print (min(listS))
#max(s) s中最大值 此时 s中的数据是可比较的 同类型
print (max(listS))
# s.index(x[,i[,j]]) 在s中或范围[i,j]中寻找首次出现x的元素编号, 当x不在其中是, index返回 ValueError
print (listS.index(9))
print (listS.index(9,5,20))
# s.count(x) x在s中的数量
print (listS.count(10))
#运行结果
[0, 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]
5 9
[5, 6, 7, 8]
[0, 5, 10, 15, 20]
30
0
29
9
9
1

# Sequence Operations for Mutable Sequence Types
#  s[i] = x 更改序列中的对应项的值
list1[1] = 100
print (list1)
# s[i:j] = t  slice of s from i to j is replaced by the contents of the iterable t
t = list(range(10))
listS[10:20]=t
print ("listS[10:20]",listS)
# s[i:j:k] = t the elements of s[i:j:k] are replaced by those of t
 
 
listS[1 :20 :2] = t
print ("listS[1 :20 :2]",listS)
# 运行结果
['python', 100, 100, 3.14159]
listS[10:20] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
listS[1 :20 :2] [0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 0, 5, 2, 6, 4, 7, 6, 8, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
 
 # del s[i:j] 删除[i,j] 的元素 
  
 del  
 listS[ 
 0 
 : 
 10 
 ]  
 
print(listS)# del s[i:j:k] 删除[i,j] 的元素 步长k
del listS[0:10:2]
print(listS) # s.append(x) 在序列末尾添加元素 值为x listS.append( 100) print(listS) # s.clear() 清空s listS.clear()
#运行结果
[0, 5, 2, 6, 4, 7, 6, 8, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 100]

# s.copy() 浅拷贝 拷贝父对象, 列表元素的父对象是地址
listForCopy = ['a', 'b', 'c', [1, 2, 3]]
listCopy = listForCopy.copy()
listForCopy[2] = 'x'
listForCopy[3][0] = 'test'
print ("listForCopy:",listForCopy)
print ("listCopy:",listCopy)
#运行结果
listForCopy: ['a', 'b', 'x', ['test', 2, 3]]
listCopy: ['a', 'b', 'c', ['test', 2, 3]]

# s.extend(t) or s += t 用t将s扩展
list1.extend(listCopy)
print (list1)
# s *= n 将s重复n次
listCopy *=3
print ("listCopy:",listCopy)
# s.insert(i, x) 在位置i处插入x
list1.insert(0,'insert')
print (list1)
# s.pop([i]) 弹除元素i ,从后面操纵从-1开始
list1.pop(1)
print (list1)
# s.remove(x) 删除第一个x
list1.remove(100)
print (list1)
# s.reverse() 反转列表
list1.reverse()
print (list1)

#运行结果
['python', 100, 100, 3.14159, 'a', 'b', 'c', ['test', 2, 3]]
listCopy: ['a', 'b', 'c', ['test', 2, 3], 'a', 'b', 'c', ['test', 2, 3], 'a', 'b', 'c', ['test', 2, 3]]
['insert', 'python', 100, 100, 3.14159, 'a', 'b', 'c', ['test', 2, 3]]
['insert', 100, 100, 3.14159, 'a', 'b', 'c', ['test', 2, 3]]
['insert', 100, 3.14159, 'a', 'b', 'c', ['test', 2, 3]]
[['test', 2, 3], 'c', 'b', 'a', 3.14159, 100, 'insert']

猜你喜欢

转载自blog.csdn.net/pengjc2001/article/details/68944029