基本形式为:
mylist = ["hg一号", "hg二号", "hg三号"]
(1)列表是使用方括号创建的:
mylist = ["hg一号", "hg二号", "hg三号"]
print(mylist)
(2)创建新列表时也可以使用 list() 构造函数。
thislist = list(("apple", "banana", "cherry"))
print(thislist)
(3)允许重复
mylist = ["hg一号", "hg二号", "hg三号", "hg三号"]
print(mylist)
列表项是有序的、可变的,并允许重复值。列表项被索引,第一项有索引[0],第二项有索引[1]等等。
(1)单个索引
例如我要索取hg一号:
mylist = ["hg一号", "hg二号", "hg三号", "hg三号"]
print(mylist)
print(mylist[0])
(2)遍历列表
for i in mylist:
print(i)
(3)列表长度
确定列表有多少项,请使用以下 len()函数:
thislist = ["hg一号", "hg二号", "hg三号", "hg三号"]
print(len(thislist))
(4)列表项 - 数据类型
列表项可以是任何数据类型。例如:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
print(list1)
print(list2)
print(list3)
(5)与元组、集合和字典的区别
要将值添加到列表的末尾,请使用append() 方法:
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
mylist.append("憨批hg")
print(mylist)
要将值添加到列表的指定位置,请使用insert() 方法:
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
mylist.insert(2,'hg菜鸟')
print(mylist)
要将另一个列表中的元素附加到当前列表,请使用extend()方法。
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
mylist1 = ["hg一号", "hg二号", "hg三号","hg四号"]
mylist.extend(mylist1)
print(mylist)
extend()方法不一定要列表,您也可以添加任何可迭代对象(元组、集合、字典等)。
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
mylist2=("hg","菜鸟")
mylist.extend(mylist2)
print(mylist)
remove()方法删除指定的项目。
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
mylist.remove('hg二号')
print(mylist)
(1)pop()方法删除指定的索引。
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
mylist.pop(2)
print(mylist)
如果不指定索引,该pop()方法默认将删除最后一项。
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
mylist.pop()
print(mylist)
(2)del删除指定的索引
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
del mylist[0]
print(mylist)
该del关键字也可以完全删除列表。
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
del mylist
(3)清空列表
clear()方法清空列表。该列表仍然存在,但没有内容。
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
mylist.clear()
print(mylist)
列表项已编入索引,您可以通过引用索引号来访问它们:
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
print(mylist[0])
注意:第一项的索引为 0。
负索引意味着从头开始,-1指最后一项, -2指倒数第二项等。
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
print(mylist[-1])
mylist = ["hg一号", "hg二号", "hg三号"]
print(mylist[1:3])
mylist = ["hg一号", "hg二号", "hg三号"]
print(mylist[-3:-1])
要确定列表中是否存在指定的项目,请使用in关键字。
例如,检查列表中是否存在“apple”:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
要更改特定位置的值,需要通过索引号:
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
mylist[0]="hg五号"
print(mylist[0])
索引号需要用范围表示。
mylist = ["hg一号", "hg二号", "hg三号","hg四号","hg五号"]
mylist[1:3]=["哈皮hg","憨批hg"]
print(mylist)
(1)循环遍历列表您可以使用循环遍历列表项for 。
mylist = ["hg一号", "hg二号", "hg三号","hg四号","hg五号"]
for i in mylist:
print(i)
(2)遍历索引号
mylist = ["hg一号", "hg二号", "hg三号","hg四号","hg五号"]
for i in range(len(mylist)):
print(mylist[i])
mylist = ["hg一号", "hg二号", "hg三号","hg四号","hg五号"]
i = 0
while i < len(mylist):
print(mylist[i])
i += 1
例如:根据fruits列表,您需要一个新列表,其中仅包含名称中带有字母“a”的fruits。
如果没有列表理解,您将不得不编写一个for带有条件测试的语句:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
使用列表推导式,你只需要一行代码即可!
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
(这么理解:先执行for循环,再执行if判断,将判断结果存储再a变量,打印出来就是个列表了)
换一个例子,只接受小于 5 的数字:
newlist = [x for x in range(10) if x < 5]
print(newlist)
把“banana”替换成"orange":(这个不是很好理解,多想想)
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x if x != "banana" else "orange" for x in fruits]
print(newlist)
默认情况下,该sort()方法区分大小写,导致所有大写字母都排在小写字母之前:
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.sort()
print(thislist)
如果你想要一个不区分大小写的排序函数,使用 str.lower 作为键函数:
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.sort(key = str.lower)
print(thislist)
reverse()方法反转元素的当前排序顺序。
mylist = ["川川一号", "川川二号", "川川三号","川川四号"]
mylist.reverse()
print(mylist)
copy()方法制作列表的副本:
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
my = mylist.copy()
print(my)
list()方法制作列表的副本:
mylist = ["hg一号", "hg二号", "hg三号","hg四号"]
my = list(mylist)
print(my)
(1)最简单的方法之一是使用+ 运算符。
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
(2)借助for循环
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
(3)extend() 方法,其目的是将元素从一个列表添加到另一个列表。
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
append() | 列表末尾添加 |
clear() | 清空列表内容 |
copy() | 复制列表 |
count() | 返回列表元素个数 |
extend() | 列表添加元素 |
index() | 返回指定元素的第一个位置 |
insert() | 指定位置插入元素 |
pop() | 删除指定位置元素 |
remove() | 删除指定元素 |
reverse() | 颠倒顺序 |
sort() | 排序 |
Q1:打印fruits列表中的第二项。
fruits = ["apple", "banana", "cherry"]
print(fruits[1])
Q2:将fruits列表中的值从“apple”改为“kiwi”。
fruits = ["apple", "banana", "cherry"]
fruits[0]="kiwi"
print(fruits)
Q3:使用 append 方法将“orange”添加到fruits列表中。
fruits = ["apple", "banana", "cherry"]
fruits.append ("orange")
print(fruits)
Q4:使用插入方法将“柠檬”添加为fruits列表中的第二项。
fruits = ["apple", "banana", "cherry"]
fruits.insert(2,"lemon")
print(fruits)
Q5:使用 remove 方法从fruits列表中删除“banana”。
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits)
Q6:使用负索引打印列表中的最后一项。
fruits = ["apple", "banana", "cherry"]
print(fruits[-1])
Q7:使用索引范围打印列表中的第三、第四和第五项。
fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(fruits[2:5])
Q8:使用正确的语法打印列表中的项目数。
fruits = ["apple", "banana", "cherry"]
for i in fruits:
print(i)
answer都经过python调试。
参考资料
https://chuanchuan.blog.csdn.net/article/details/120419754?spm=1001.2014.3001.5502
用Python玩转数据——中国大学mooc