Python学习笔记 - 列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习笔记 - 列表相关的知识,希望对你有一定的参考价值。

# -----------------------------------------------------
# 创建列表 : [], 用方括号标识列表, 用逗号来分割元素
motorcycles = [‘honada‘,‘yamaha‘,‘suzuki‘]
print(motorcycles)  #[‘honada‘, ‘yamaha‘, ‘suzuki‘]


# -----------------------------------------------------
# 修改列表 : 列表[索引]
motorcycles[0] = ‘ducati‘
print(motorcycles) #[‘ducati‘, ‘yamaha‘, ‘suzuki‘]

motorcycles[1] = "xingfu125"
print(motorcycles)  #[‘ducati‘, ‘xingfu125‘, ‘suzuki‘]


# -----------------------------------------------------
# 列表.append() : 向列表末尾添加元素
motorcycles.append(‘yamaha‘)
print(motorcycles)  #[‘ducati‘, ‘xingfu125‘, ‘suzuki‘, ‘yamaha‘]


# -----------------------------------------------------
# 动态创建列表
names = []
names.append("Tom")
names.append("Jerry")
names.append("John")
print(names)    #[‘Tom‘, ‘Jerry‘, ‘John‘]


# -----------------------------------------------------
# 列表.insert() : 向列表中插入元素(元素可以是异质的)
numbers = list(range(0,10))
print(numbers)  #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers.insert(1,88)
print(numbers)  #[0, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers.insert(0,‘python:‘) #插入一个字符串
print(numbers)  #[‘python:‘, 0, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9]


# -----------------------------------------------------
# del 列表[索引] : 删除列表中的某个索引对应的元素
# numbers = [‘python:‘, 0, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 获取列表numbers中"python:"的索引
index = numbers.index("python:") 
print(‘index = {0}‘.format(index))   # index = 0

del numbers[index]  #从列表numbers中删除元素"Python:"
print(numbers)  # numbers = [0, 88, 1, 2, 3, 4, 5, 6, 7, 8, 9]

del numbers[0::2]     #从索引0开始以步长2删除列表numbers中的元素
print(numbers)  # numbers = [88, 2, 4, 6, 8]



# -----------------------------------------------------
# 列表.pop() : 弹出列表中的元素(任何位置)
lst = list(range(10,20))
print(lst)  # lst = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

# 默认弹出末尾元素
lst_pop = lst.pop() # lst_pop = 19
print(lst)  # lst = [10, 11, 12, 13, 14, 15, 16, 17, 18]

# 弹出列表中指定索引的元素
lst_pop = lst.pop(0)    # lst_pop = 10
print(lst)  # lst = [11, 12, 13, 14, 15, 16, 17, 18]

lst_pop = lst.pop(4)    # lst_pop = 15
print(lst)  # lst = [11, 12, 13, 14, 16, 17, 18]


# -----------------------------------------------------
# len() : 获取列表的元素个数
lst = [11, 12, 13, 14, 16, 17, 18]
num = len(lst)
print(‘num = %d‘ % num) # num = 7


# -----------------------------------------------------
# 列表.remove() : 根据值删除元素(如果值有多个, 那么只能删除一个)
lst = [11, 12, 13, 14, 14, 14, 16, 17, 18]
lst_pop = lst.remove(14)
print(lst) # [11, 12, 13, 14, 14, 16, 17, 18]
print(lst_pop) # None


# -----------------------------------------------------
# 列表.sort() : 对列表进行排序
cars = [‘BMW‘, ‘AUDI‘,‘TOYOTA‘,‘BUICK‘]
cars.sort()
print(cars) # [‘AUDI‘, ‘BMW‘, ‘BUICK‘, ‘TOYOTA‘] 按ASCII码

# 反向排序, 指定sort()参数为: reverse = True
cars.sort(reverse = True)
print(cars) # [‘TOYOTA‘, ‘BUICK‘, ‘BMW‘, ‘AUDI‘]

# sorted(列表) : 对列表临时排序(既能按特定顺序显示列表元素, 又不改变原始排序)
cars = [‘BMW‘, ‘AUDI‘,‘TOYOTA‘,‘BUICK‘]
cars_sorted = sorted(cars)
print(cars_sorted)     # [‘AUDI‘, ‘BMW‘, ‘BUICK‘, ‘TOYOTA‘]
print(cars)            # [‘BMW‘, ‘AUDI‘, ‘TOYOTA‘, ‘BUICK‘]

# -----------------------------------------------------
# 列表.reverse() : 反转列表
names = [‘zhangsan‘, ‘lisi‘, ‘wangwu‘, ‘zhaoliu‘]
names_reverse = names.reverse()
print(names)            # [‘zhaoliu‘, ‘wangwu‘, ‘lisi‘, ‘zhangsan‘]
print(names_reverse)    # None

# -----------------------------------------------------
# 复制列表
names = [‘zhangsan‘, ‘lisi‘, ‘wangwu‘, ‘zhaoliu‘]
names_copy = names[:]
print(names_copy)   # [‘zhaoliu‘, ‘wangwu‘, ‘lisi‘, ‘zhangsan‘]

# -----------------------------------------------------
# 遍历列表
names = [‘zhangsan‘, ‘lisi‘, ‘wangwu‘, ‘zhaoliu‘]
for name in names:
    print(‘name = {0}‘.format(name))    # ‘name = zhangsan‘
                                        # ‘name = lisi‘
                                        # ‘name = wangwu‘
                                        # ‘name = zhaoliu‘

 

以上是关于Python学习笔记 - 列表的主要内容,如果未能解决你的问题,请参考以下文章

python学习笔记012——pdb调试

python学习笔记第二天

30 段 Python 实用代码

Python学习笔记 列表生成式_生成器

Python 3学习笔记

Python学习笔记之列表和元组