Python学习【第3篇】:列表魔法

##########################深灰魔法-list类中提供的方法##################
#list 类,列表
list = [1,12,9,"age",["测试",["19",10],"小星"],"xiaoxing",True]
#通过list创建的对象
#1.列表格式
#2.列表中可以嵌套任何类型
#中括号括起来
#,分割每个元素
#列表中的元素可以是数字,字符串,列表,布尔值....所有都能放进去
#"集合",内部放置任何东西

#3.索引取值
print(list[3])
#4.切片取值,切片结果也是列表
print(list[3:5])


#5.for while循环取值
for intem in list:
print(intem)

运行结果:

age
['age', ['测试', ['19', 10], '小星']]

1
12
9
age
['测试', ['19', 10], '小星']
xiaoxing
True

 

 

list = [1,12,9,"age",["测试",["19",10],"小星"],"xiaoxing",True]

###########索引
#6.修改
list[1] = 120
print(list)
运行结果:
[1, 120, 9, 'age', ['测试', ['19', 10], '小星'], 'xiaoxing', True]

list[1] = [11,22,33,44]
print(list)
运行结果:
[1, [11, 22, 33, 44], 9, 'age', ['测试', ['19', 10], '小星'], 'xiaoxing', True]
############################
#删除,第一种方式
del list[1]
print(list)

###########7.切片
#修改
list[1:3] = [520,520]
print(list)

#删除
del list[2:6]
print(list)
三个运行结果:

[1, 9, 'age', ['测试', ['19', 10], '小星'], 'xiaoxing', True]
[1, 520, 520, ['测试', ['19', 10], '小星'], 'xiaoxing', True]
[1, 520]

 

list = [1,12,9,"age",["测试",["19",10],"小星"],"xiaoxing",True]
#8. in操作
v = "xiaoxing" in list
print(v)

v1 = "19" in list
print(v1)

v2 = "测试" in list[4]
print(v2)
三个运行结果分别是

True
False
True




#9.操作,取列表类的元素
list = [1,12,9,"age",["测试",["19",10],"小星"],"xiaoxing",True]
v = list[4][1][0]
print(v)
运行结果
19

a = "123"
#把字符串转换成整型
v1 = int(a)
print(v1)
运行结果:
123

s = "xiaoxingasdf"
#把字符串转换为列表,把每个字符都拿出来当成列表的元素,相当于做了for循环
v2 = list(s)
print(v2)
['x', 'i', 'a', 'o', 'x', 'i', 'n', 'g', 'a', 's', 'd', 'f']

s = 123
#由于数字不能被循环和迭代,因此在这里运行会报错
v3 = list(s)
print(v3)

 

#转换
#字符串转换列表 v = list() ,内部使用for循环

list = [11,12,22,"123","xiaoxing"]
#把列表转换成字符串
v = str(list)#相当于把列表看成整体'[11,12,22,"123","xiaoxing"]'
print(v)
运行结果:
[11, 12, 22, '123', 'xiaoxing']

#但是这里的运行结果是一个整体,实际上只想想显示真正的字符串
#此时就需要自己写for 循环,一个一个处理了,既有数字又有字符串
list = [11,12,22,"123","xiaoxing"]
s = ""
for i in list:
s = s +str(i)
print(s)
运行结果:
111222123xiaoxing

list = ["123","456"]
#当列表中的元素只有字符串的时候,直接使用字符串的join方法
v = "".join(list)
print(v)
运行结果:
123456




li = ["123","456","789"]
#参数
#1.在原来值最后追加
li.append(5)
li.append("xiaoxing")
li.append([123,321])
print(li)
运行结果:
['123', '456', '789', 5, 'xiaoxing', [123, 321]]
li = ["123","456","789"]
li.clear()
#2.清空列表
print(li)
运行结果:
[]

li = ["123","456","789"]
#3.拷贝,浅拷贝
v = li.copy()
print(v)
运行结果:
['123', '456', '789']

li = ["123","456","789","123"]
#4.计算元素出现的次数
v = li.count("123")
print(v)
运行结果:
2

#5.扩展原列表,参数:需是可迭代对象
li = [11,22,33,22,44]
li.append([112,"测试"])
print(li)
运行结果:
[11, 22, 33, 22, 44, [112, '测试']]

li = [11,22,33,22,44]
li.extend([9898,"测试"])
print(li)
#这里面相当于for i in [9898,"测试"]:
# li.append(i)
运行结果:
[11, 22, 33, 22, 44, 9898, '测试']

#6.根据值获取当前索引位置(左边优先)
li = [11,22,33,22,44]
v = li.index(22)
print(v)
运行结果:
1

li = [11,22,33,22,44]
#7.在指定索引位置插入值
li.insert(0,120)
print(li)
运行结果:
[120, 11, 22, 33, 22, 44]

li = [11,22,33,22,44]
#8.删除某个值(1.指定索引;2.默认删除最后一个值)
v1 = li.pop()
v2 = li.pop(1)
print(v1,v2)
print(li)
运行结果:

44 22
[11, 33, 22]

 

li = [11,22,33,22,44]
#9.删除列表中指定值,左边优先
li.remove(22)
print(li)
运行结果:
[11, 33, 22, 44]

li = [11,22,33,22,44]
#10.将当前列表反转
li.reverse()
print(li)
运行结果:
[44, 22, 33, 22, 11]

li = [11,22,44,33,22]
#11.列表的排序
li.sort()
#从小到大排
print(li)
运行结果:
[11, 22, 22, 33, 44]

li.sort(reverse=True)
#从大到小排序
print(li)
运行结果:
[44, 33, 22, 22, 11]




 

posted on 2018-05-19 07:56  小星1995  阅读(170)  评论(0编辑  收藏  举报

导航