列表
前面两篇文章关于Python最基础的数据,今天这篇文章的主题是列表。
列表用于有序存储数据,包括数字、字母、字符串等。
(1)创建与打印列表
smart_products=['computer','phone','television']
print(smart_products)
运行结果:
['computer','phone','television']
打印列表元素:
print(smart_products[0])
运行结果:
computer
打印列表元素:
print(smart_products[-1])
运行结果:
television
(2)修改列表元素
smart_products=['computer','phone','television']
smart_products[1]='tablet'
print(smart_products)
运行结果:
['computer','tablet','television']
(3)添加列表元素
smart_products=['computer','phone','television']
smart_products.append('tablet')
print(smart_products)
运行结果:
['computer','phone','television','tablet']
插入元素:
smart_products=['computer','phone','television']
smart_products.insert(1,'tablet')
print(smart_products)
运行结果:
['computer','tablet','phone','television']
(4)删除列表元素
smart_products=['computer','phone','television']
del smart_products[1]
print(smart_products)
运行结果:
['computer','television']
弹出列表元素:
smart_products=['computer','phone','television']
popped_product=smart_products.pop()
print(smart_products)
print(popped_product)
运行结果:
['computer','phone']
television
根据索引弹出列表元素:
smart_products=['computer','phone','television']
popped_product=smart_products.pop(1)
print(smart_products)
print(popped_product)
运行结果:
['computer','television']
phone
根据值弹删除出列表元素:
smart_products=['computer','phone','television']
removed_product='television'
smart_products.remove(removed_product)
print(smart_products)
print(removed_product)
运行结果:
['computer','phone']
television
(4)列表排序
按字母顺序排序:
internet_giants=['baidu','alibaba','tencent','google','ibm','microsoft','lenovo','apple']
internet_giants.sort()
print(internet_giants)
运行结果:
['alibaba','apple','baidu','google','ibm','lenovo','microsoft','tencent']
倒着按字母顺序排序:
internet_giants=['baidu','alibaba','tencent','google','ibm','microsoft','lenovo','apple']
internet_giants.sort(reverse=True)
print(internet_giants)
运行结果:
['tencent','microsoft','lenovo','ibm','google','baidu','apple','alibaba']
临时按字母顺序排序:
internet_giants=['baidu','alibaba','tencent','google','ibm','microsoft','lenovo','apple']
internet_giants_1=sorted(internet_giants)
print(internet_giants)
print(internet_giants_1)
运行结果:
['baidu','alibaba','tencent','google','ibm','microsoft','lenovo','apple']
['alibaba','apple','baidu','google','ibm','lenovo','microsoft','tencent']
倒着按字母顺序临时排序:
internet_giants=['baidu','alibaba','tencent','google','ibm','microsoft','lenovo','apple']
internet_giants_1=sorted(internet_giants,reverse=True)
print(internet_giants)
print(internet_giants_1)
运行结果:
['baidu','alibaba','tencent','google','ibm','microsoft','lenovo','apple']
['tencent','microsoft','lenovo','ibm','google','baidu','apple','alibaba']
翻转列表:
internet_giants=['baidu','alibaba','tencent','google','ibm','microsoft','lenovo','apple']
internet_giants.reverse()
print(internet_giants)
运行结果:
['apple','lenovo','microsoft','ibm','google','tencent','alibaba','baidu']
(5)计算列表长度
internet_giants=['baidu','alibaba','tencent','google','ibm','microsoft','lenovo','apple']
length=len(internet_giants)
print(length)
运行结果:
8
(6)使用for循环遍历整个列表
注意缩进和冒号
cities=['shanghai','paris','london','new york']
for city in cities:
print(city.title())
运行结果:
Shanghai
Paris
London
New York
(7)使用range()和list()创建数值列表
注意边界
for number in range(1,7):
print(number )
运行结果:
1
2
3
4
5
6
创建列表:
numbers=[]
for number in range(1,5):
numbers.append(number )
print(numbers)
运行结果:
[1,2,3,4]
使用list()创建列表:
print(list(range(1,5)))
运行结果:
[1,2,3,4]
指定步长:
print(list(range(1,10,2)))
运行结果:
[1,3,5,7,9]
(8)统计列表
numbers=list(range(1,10))
print(min(numbers))
print(max(numbers))
print(sum(numbers))
运行结果:
1
9
45
(9)列表解析
numbers=[n**3 for n in range(1,5)]
print(numbers)
运行结果:
1
8
27
64
(10)列表切片与列表复制
列表切片:
fruits=['apple','banana','grape','orange']
fruits_1=fruits[1:3]
fruits_2=fruits[1:]
fruits_3=fruits[:3]
fruits_4=fruits[-2:]
print(fruits)
print(fruits_1)
print(fruits_2)
print(fruits_3)
print(fruits_4)
运行结果:
['apple','banana','grape','orange']
['banana','grape']
['banana','grape','orange']
['apple','banana','grape']
['grape','orange']
复制列表:
使用切片而非列表名复制列表:
fruits=['apple','banana','grape','orange']
fruits_1=fruits[:]
fruits_1.append('cherry')
print(fruits_1)运行结果:
['apple','banana','grape','orange','cherry']
今天信息量有点大,回顾了列表的创建、改变、添加、删除,列表本身的排序与字母排序,列表长度的计算,列表的循环遍历,数值列表的创建、简单统计,列表的解析,列表切片与复制。其中,重要的几点有,for循环,del语句,方法append()、insert()、pop()、remove()、sort()、reverse(),函数sorted()、len()、min()、max()、sum()、range()、list()。这些函数和方法在以后的学习中也会经常用到。