python


# -*- coding:utf-8 -*-

'''
@project: jiaxy
@author: Jimmy
@file: study_3_列表.py
@ide: PyCharm Community Edition
@time: 2018-11-01 17:11
@blog: https://www.cnblogs.com/gotesting/

'''


# 列表 list
# []
# 1. 空元组
# 2. 元素之间用,分开
# 3. 列表可以存放任意类型的数据

l = []
m = [1]
n = [1,]
print(type(l),type(m),type(n))

# 4. 取单个元素 list[index] index从0开始
a = [1,0.02,'Jimmy',('summer',123,'sunny'),['PUBG',520,1314]]

b= a[3]
print(b)

# 5. 支持切片,同字符串操作
c = a[0:5:2]
print(c)

# 6. 嵌套
d = a[-2][-1]
print(d)

# 7. 列表 可增删改查
# list.append(x) 在列表的最后面添加元素x,每次只能添加一个
# list.insert(index,y) 在列表的index位置添加元素y,index前
a.append('waw3')
print(a)
a.insert(1,'LOL')
print(a)
# list.pop(x) 根据指定索引,删除指定元素,为空时删除最后一个
a.pop(1)
print(a)
# list[index]=x,将指定索引位置的元素修改为x
a[1] = 1.23
print(a)


# 8. 排序
# list.sort() 纯数字从小到大排序
# list.reverse() 倒序排列
l = [9,8,6,7,4,5,1,2,3]
l.sort()
print(l)
l.reverse()
print(l)


# list.count()
print(l.count(1))
# list.index()
print(l.index(1))
# list.copy() 复制
m = l.copy()
print(m)
# list.extend 拓展
l.extend(a)
print(l)
# list.remove(x) 删除x元素,每次只能删除一个
l.remove(9)
print(l)
# list.clear() 清除所有元素
l.clear()
print(l)



原文地址:https://www.cnblogs.com/gotesting/p/9890739.html