python基础--列表---基础概念和使用
发布时间
阅读量:
阅读量
一、列表
定义:由一定顺序排列成的元素构成一个有序序列。在运用列表时需注意避免索引错误
表示:以方括号[]的形式标记内容,并内部分割用逗号分隔。例如:[a, b, c]
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
代码解读
二、使用列表
通过获取指定索引处的项目来进行操作
列表的索引是从0开始的
def print_hi():
bicycles = ['trek','cannondale','redline','specialized']
print(bicycles[0])
代码解读
输出
trek
代码解读
列表项调整需遵循以下步骤:首先明确目标列表名称;其次确定需调整的具体项及其对应的索引号;最后设置其新的内容。
def print_hi():
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
#修改列表第一个元素,并且打印列表
motorcycles[0] = 'ducati'
print(motorcycles)
代码解读
输出
#修改前
['honda', 'yamaha', 'suzuki']
#修改后
['ducati', 'yamaha', 'suzuki']
代码解读
在列表尾部添加元素的方式是通过append()函数实现的。这种方式能够有效地将指定的元素追加到列表的末尾位置
def print_hi():
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
#使用append()在列表尾部添加元素
motorcycles.append('ducati')
print(motorcycles)
代码解读
输出
['honda', 'yamaha', 'suzuki']
#尾部添加元素输出
['honda', 'yamaha', 'suzuki', 'ducati']
代码解读
该函数insert()可用于在列表中的任意位置插入新元素。然而必须明确确定其索引和值。
def print_hi():
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
motorcycles.insert(0,'ducati')
print(motorcycles)
代码解读
输出
['honda', 'yamaha', 'suzuki']
['ducati', 'honda', 'yamaha', 'suzuki']
代码解读
4、删除元素
(1)使用del语句,将删除的元素从列表对应的位置上移除
def print_hi():
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
代码解读
输出
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']
代码解读
(2)pop()弹出列表中元素
从列表中删除最后一个元素,其实质是从该列表中移除最后的一个项;因此,在调用lists.pop()时所获取的值即为该列表当前末尾的那个元素。
def print_hi():
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
popedlists = motorcycles.pop()
print(motorcycles)
print(popedlists)
代码解读
输出
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
suzuki
代码解读
用pop()函数来删除列表中的任意位置元素,则仅需在函数调用括号内明确指定待删元素的位置信息
def print_hi():
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
first_owned = motorcycles.pop(1)
print("The first motorcycles I owned was a "+first_owned)
#再次打印motorcycles观察变化
print(motorcycles)
代码解读
输出
['honda', 'yamaha', 'suzuki']
The first motorcycles I owned was a yamaha
['honda', 'suzuki']
代码解读
(3)根据值删除元素
使用remove()方法,将值从列表中移除
def print_hi():
motorcycles = ['honda','yamaha','ducati','suzuki']
print(motorcycles)
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print("\nA "+too_expensive.title()+" is too expensive for me")
print(motorcycles)
代码解读
输出
['honda', 'yamaha', 'ducati', 'suzuki']
A Ducati is too expensive for me
['honda', 'yamaha', 'suzuki']
代码解读
三、组织列表
- sort()方法会对列表执行全局性排序操作
说明:请注意,默认情况下,默认设置下sort()方法会将list中的元素按字母顺序排列。若希望将list中的元素按相反字母顺序排列,则需要在调用sort()方法时传递reverse=True参数即可实现
()默认情况下会将它们按字母顺序排列)
def print_hi():
cars = ['bwm','audi','toyota','subaru']
cars.sort()
print(cars)
代码解读
输出
['audi', 'bwm', 'subaru', 'toyota']
代码解读
(2)按照字母相反顺序排序
def print_hi():
cars = ['bwm','audi','toyota','subaru']
cars.sort(reverse=True)
print(cars)
代码解读
['toyota', 'subaru', 'bwm', 'audi']
代码解读
调用sorted()函数会对指定列表进行临时排序操作。调用该函数后不会改变原列表的排序状态。如需以逆序显示字母顺序,则可向sorted()函数传递参数reverse=True。
def print_hi():
cars = ['bwm','audi','toyota','subaru']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print("\nHere is the original list again:")
print(cars)
代码解读
输出
Here is the original list:
['bwm', 'audi', 'toyota', 'subaru']
Here is the sorted list:
['audi', 'bwm', 'subaru', 'toyota']
Here is the original list again:
['bwm', 'audi', 'toyota', 'subaru']
代码解读
3、reverse()永久性想列表元素顺序反转
def print_hi():
cars = ['bwm','audi','toyota','subaru']
print(cars)
cars.reverse()
print(cars)
代码解读
输出
['bwm', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bwm']
代码解读
4、len(列表名)获取列表长度
def print_hi():
cars = ['bwm','audi','toyota','subaru']
print(cars)
print("The length of cars is " + str(len(cars)))
代码解读
输出
['bwm', 'audi', 'toyota', 'subaru']
The length of cars is 4
代码解读
全部评论 (0)
还没有任何评论哟~
