定义空list,定义list不能使用使用list关键字 List_1 = [] 定义包含数据的list List_2 = [1,2.0,3+4j,”abc”,(1,2,3),[5,6],{“username”:”hhq”,”password”:”123456”},{9,8,7}]
List生成一个列表
list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] list("abcd") ['a', 'b', 'c', 'd'] 增加元素 append 在列表末尾追加元素 List_2 = [1,2.0,3+4j,"abc",(1,2,3),[5,6],{"username":"hhq","password":"123456"},{9,8,7}] List_2.append("XYZ") List_2 [1, 2.0, (3+4j), 'abc', (1, 2, 3), [5, 6], {'username': 'hhq', 'password': '123456'}, {8, 9, 7}, 'XY Z'] 查看list类型 type(List_2) <class 'list'>
Insert(index,item)在指定位置插入元素
List_2.insert(0,"400") List_2 ['400', 1, 2.0, (3+4j), 'abc', (1, 2, 3), [5, 6], {'username': 'hhq', 'password': '123456'}, {8, 9, 7}, 'XYZ']
a.extend(b) 把b的每个元素追加到列表a
a =[1,2,3] b=["a","b","c"] 追加列表 a.extend(b) a [1, 2, 3, 'a', 'b', 'c']
追加字符串
a.extend(("xyz")) a [1, 2, 3, 'a', 'b', 'c', 'x', 'y', 'z']
追加元组
a.extend((4,5,6)) a [1, 2, 3, 'a', 'b', 'c', 'x', 'y', 'z', 4, 5, 6]
追加集合
a.extend({"X","Y"}) a [1, 2, 3, 'a', 'b', 'c', 'x', 'y', 'z', 4, 5, 6, 'X', 'Y']
追加字典 注意:只会字典的key追加到列表a中
a.extend({"username":"hhq","passwd":"123456"}) a [1, 2, 3, 'a', 'b', 'c', 'x', 'y', 'z', 4, 5, 6, 'X', 'Y', 'username', 'passwd']
小练习: 把以下l的a,d,g取出,输出字符串”adg”
l = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
方式1:
string = "" for c in l[::3]: ... string += c ... print(string) adg
方式2:
"".join(l[::3])#join把列表转换为字符串 'adg'
更新列表元素: 更新一个元素值 ['a', 'b', 'c', 'd', 'e', 'f', 'g']
l[0] = "A" l ['A', 'b', 'c', 'd', 'e', 'f', 'g']
更新连续的多个元素值
list_1[:2] = [3,4] list_1 [3, 4, 'c', 'd', 'e', 'f', 'g']
**删除列表元素 删除单个列表元素
result ['a', 'b', 'c', 2, 3, 4, 5, 6, 7, 8, 9] del result[0] result ['b', 'c', 2, 3, 4, 5, 6, 7, 8, 9]
删除连续的多个列表元素
del result[0:2] result [2, 3, 4, 5, 6, 7, 8, 9]
删除所有列表元素但不删除列表
del result[:] 删除整个列表 del result
pop([item])删除元素 item可选参数 如果带item,直接删除item元素 如果不带item删除末尾的一个元素并返回
带参数
l = list(range(10)) l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] item = l.pop()#没带参数直接删除末尾元素 item 9 l [0, 1, 2, 3, 4, 5, 6, 7, 8]
指定值
l.pop(5)#指定值,直接删除指定值 5 l [0, 1, 2, 3, 4, 6, 7, 8]
clear()清空列表
n.clear()#等价于del n[:] n []
remove(item) 删除指定的列表元素item
list_2 [9, 8, 3, 2, 1, 0] list_2.remove(9) list_2 [8, 3, 2, 1, 0]
小练习: 实现一个小的图书馆程序
info = """ add: to add a book the library lend: to lend a book from getall: to list all books .:to exit """ print(info) book_list = [] while True: command = input("please input the command: add or lend or . exit: ") if command == "add": book_name = input("please input add book: ") book_list.append(book_name) elif command == "lend": book_name = input("please input lend book: ") if book_name in book_list: book_list.remove(book_name) else: print ("not exists") elif command == "getall": if book_list:
print("book list:",book_list)
else:
print("no book")
elif command ==".":
Break
其他操作 count(item)计算item在列表中的次数
n = list(range(10)) n.count(3) 1
Index(item) 求元素在列表中第一次出现的索引位置
n.index(2) 2
排序 sorted(list) 不改变原有list的顺序 有返回值,返回排序后的列表
a = [1,2,3,1,4,9,3] sorted_a = sorted(a) sorted_a [1, 1, 2, 3, 3, 4, 9]
list.sort() 直接把原列表排序,返回值是None
a.sort() a [1, 1, 2, 3, 3, 4, 9] a
list.reverse() 翻转元素但不排序
a.reverse() a [9, 4, 3, 3, 2, 1, 1]
sort() sorted()实现降序排序 只需要添加reverse=True即可
list_1 = [1,3,4,2,0,9] sorted_l = sorted(list_1,reverse=True) sorted_l [9, 4, 3, 2, 1, 0]
list_2 = [3,2,1,0,8,9] list_2.sort(reverse=True) list_2 [9, 8, 3, 2, 1, 0]
访问列表元素 利用列表索引访问列表元素
list_1 = list(range(10)) list_1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 0
list_1[-1] 9
list_1[0:2] [0, 1]
list_1[0:] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list_1[:3] [0, 1, 2]
list_1[:] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list_1[-3:-1] [7, 8]
指定步长,访问偶数索引位置元素
list_1[0::2] [0, 2, 4, 6, 8]
指定步长,访问奇数索引位置元素
list_1[1::2] [1, 3, 5, 7, 9]
|