码农之家
这篇文章主要知识点是关于Python编程,从入门到实践,列表简介,Python编程:从入门到实践,18天学习《python编程:从入门到实践》心得笔记 Python编程学习之如何判断3个数的大小 《Python编程:从入门到实践》第九章:类 《Python编程:从入门到实践》DEMO实例代码 python编程中最常用的比较运算符实例 的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下电子书
官方说明就是由一些列按特点顺序排列的元素组成。其实可以看出很多个字符串的有序组合吧,里面的内容可以随时的删除,增加,修改。
下面这个就是一个列表,python打印列表的时候会将中括号和引号打印出来的
>>> name = ['liubin','liujian','liuliu'] >>> print (name) ['liubin', 'liujian', 'liuliu']
和大多数编程语言一样,列表的第一个元素的索引是0,而不是1。如果要输出最后一个元素可以使用-1,倒数第二个可以使用-2,以此类推。
>>> language = ['python','C++','Java','PHP','Ruby'] >>> print (language[0]) python >>> print (language[2]) Java >>> print (language[-1]) Ruby >>> print (language[-2].lower()) php
修改:其实就是对元素的重新赋值
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) ['honda', 'yamaha', 'suzuki'] motorcycles[0] = 'ducati' print(motorcycles) ['ducati', 'yamaha', 'suzuki']
在末尾添加:append()
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) ['honda', 'yamaha', 'suzuki'] motorcycles.append('ducati') print(motorcycles) ['honda', 'yamaha', 'suzuki', 'ducati']
在列表中插入:insert(索引,字符串)
motorcycles = ['honda', 'yamaha', 'suzuki'] motorcycles.insert(0, 'ducati') print(motorcycles)
del语句删除元素,需要知道元素在列表中索引,删除后,无法再次访问它了
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) ['honda', 'yamaha', 'suzuki'] del motorcycles[0] print(motorcycles) ['yamaha', 'suzuki']
pop()弹出元素,默认是弹出最顶层的元素,就是最后一个,如果知道元素在列表中的位置,可以添加索引弹出
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) ['honda', 'yamaha', 'suzuki'] popped_motorcycle = motorcycles.pop() print(motorcycles) ['honda', 'yamaha'] print(popped_motorcycle) suzuki
motorcycles = ['honda', 'yamaha', 'suzuki' first_owned = motorcycles.pop(0) print('The first motorcycle I owned was a ' + first_owned.title() + '.') The first motorcycle I owned was a Honda.
有的时候你不知道元素的位置,但是知道元素的内容,可以使用remove('字符串')或者remove(变量)来删除,remove()只能删除第一个指定的值,删除的值可能在列表中出现多次,就需要使用后面学习到的循环了。
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] print(motorcycles) ['honda', 'yamaha', 'suzuki', 'ducati'] motorcycles.remove('ducati') print(motorcycles) ['honda', 'yamaha', 'suzuki']
方法sort()对列表进行永久的排序
>>> car = ['bmw','audi','toyota','subaru'] >>> print (car) ['bmw', 'audi', 'toyota', 'subaru'] >>> car.sort() >>> print (car) ['audi', 'bmw', 'subaru', 'toyota'] >>> car.sort(reverse=True) >>> print (car) ['toyota', 'subaru', 'bmw', 'audi']
向方法传递参数reverse=Ture可以倒着排序
函数sorted()对列表进行临时的排序
>>> car = ['bmw','audi','toyota','subaru'] >>> print (sorted(car)) ['audi', 'bmw', 'subaru', 'toyota'] >>> print (car) ['bmw', 'audi', 'toyota', 'subaru']
如果你要按与字母顺序相反的顺序显示列表,也可以向函数sorted()传递参数reverse=Ture
方法reverse()反正列表
>>> car = ['bmw','audi','toyota','subaru'] >>> car.reverse() >>> print (car) ['subaru', 'toyota', 'audi', 'bmw']
函数len()可以知道列表的长度,python计算元素个数的时候,是从1开始的。
>>> car = ['bmw','audi','toyota','subaru'] >>> len(car) 4
#3-1 姓名
names = ['Qiqi','Danliang','Mingliang','Peng'] for x in range(0,4): print(names[x])
#3-2 问候语
names = ['qiqi','qanliang','mingliang','peng'] for x in range(0,4): print("Hello," + names[x].title() + "大吉大利!")
#3-3 自己的列表
trip_mode = ['小黄车','摩拜','暴龙兽','大宝剑'] for x in range(0,4): print("我今天想骑" + trip_mode[x] + "去抓皮卡丘!")
#3-4 嘉宾名单
names = [] names.append('新垣结衣') names.append('奥黛丽赫本') names.append('迪丽热巴') for x in range(0,3): print('Dear ' + names[x] + ',Can you come to my dinner?')
#3-5 修改嘉宾名单
names = [] names.append('新垣结衣') names.append('奥黛丽赫本') names.append('迪丽热巴') for x in range(0,3): print('Dear ' + names[x] + ',Can you come to my dinner?') print(names[1] + '无法赴约。') names[1] = ('赵丽颖') #改名单 for x in range(0,3): print('Dear ' + names[x] + ',Can you come to my dinner?')
#3-6 添加嘉宾
names = [] names.append('新垣结衣') names.append('奥黛丽赫本') names.append('迪丽热巴') for x in range(0,3): print('Dear ' + names[x] + ',Can you come to my dinner?') print('\n' + names[1] + '无法赴约。\n') names[1] = ('赵丽颖') #改名单 for x in range(0,3): print('Dear ' + names[x] + ',Can you come to my dinner?') print('\nHaha~,I found a bigger table, I can invite more people!\n') names.insert(0,'紫霞') names.insert(2,'静香') names.append('提莫') for x in range(0,len(names)): print('Dear ' + names[x] + ',Can you come to my dinner?')
#3-7 缩减名单
names = [] names.append('新垣结衣') names.append('奥黛丽赫本') names.append('迪丽热巴') for x in range(0,3): print('Dear ' + names[x] + ',Can you come to my dinner?') print('\n' + names[1] + '无法赴约。\n') names[1] = ('赵丽颖') #改名单 for x in range(0,3): print('Dear ' + names[x] + ',Can you come to my dinner?') print('\nHaha~,I found a bigger table, I can invite more people!\n') names.insert(0,'紫霞') names.insert(2,'静香') names.append('提莫') for x in range(0,len(names)): print('Dear ' + names[x] + ',Can you come to my dinner?') print('\nOh! GouRiDe快递没给我把桌子送来,我只能邀请两位了(┬_┬)!\n') X = len(names) for x in range(0,X-2): popped_name = names.pop() print('实在抱歉,' + popped_name +'我不能与你共进晚餐了!') for x in range(0,len(names)): print('Dear ' + names[x] + ',我依然邀请你来与我共进晚餐!') del names[1] #del列表应从后往前! del names[0] print(names)
#3-8 放眼世界
cities = ['chongqing','chengdu','xiamen','kuning','shenzhen'] print(cities) print(sorted(cities)) print(cities) print(sorted(cities,reverse=True)) print(cities) cities.reverse() print(cities) cities.reverse() print(cities) cities.sort() print(cities) cities.sort(reverse=True) print(cities)
#3-9 晚餐嘉宾
names = [] names.append('新垣结衣') names.append('奥黛丽赫本') names.append('迪丽热巴') for x in range(0,3): print('Dear ' + names[x] + ',Can you come to my dinner?') print('\n' + names[1] + '无法赴约。\n') names[1] = ('赵丽颖') #改名单 for x in range(0,3): print('Dear ' + names[x] + ',Can you come to my dinner?') print('\nHaha~,I found a bigger table, I can invite more people!\n') names.insert(0,'紫霞') names.insert(2,'静香') names.append('提莫') for x in range(0,len(names)): print('Dear ' + names[x] + ',Can you come to my dinner?') print('\nOh! GouRiDe快递没给我把桌子送来,我只能邀请两位了(┬_┬)!\n') X = len(names) for x in range(0,X-2): popped_name = names.pop() print('实在抱歉,' + popped_name +'我不能与你共进晚餐了!') for x in range(0,len(names)): print('Dear ' + names[x] + ',我依然邀请你来与我共进晚餐!') print('\n最后,我将邀请' + str(len(names)) + '个人参加晚宴!') del names[1] #del列表应从后往前! del names[0] print(names)
以上就是本次给大家分享的全部知识点内容总结,大家还可以在下方相关文章里找到解决axios.interceptors.respon、 儿童python编程入门书籍推、 详解vue axios封装请求状态、 等python文章进一步学习,感谢大家的阅读和支持。
展开 +
收起 -
Copyright 2018-2019 xz577.com 码农之家