python-列表方法(list)知识整理
2018-11-22 08:44:39来源:博客园 阅读 ()
# #!/usr/bin/env python
# # -*- coding:utf-8 -*-
# # __author__:anxu.qi
# # Date:2018/11/19
# ######################### 列表 #################################
aa_list = ["tami","beijing","fengshan","fengtai","apache","nginx"]
# print(aa_list)
## 索引
print(aa_list[0])
# tami
## 切片
print(aa_list[0:2])
# ['tami', 'beijing']
## len
print(aa_list[2:len(aa_list)])
# ['fengshan', 'fengtai', 'apache', 'nginx']
################# for ################
# aa_list = ["tami","beijing","fengshan","fengtai","apache","nginx"]
## 循环
for i in aa_list:
print(i)
"""
tami
beijing
fengshan
fengtai
apache
nginx
# """
############################# 列表内布提供的功能 ###############################
################################ append 追加 ##################################
# def append(self, p_object):
# 追加功能,在列表的最后添加
name_list = ["apache","nginx","tomcat","redis"]
print(name_list)
# ['apache', 'nginx', 'tomcat', 'redis']
name_list.append("mysql")
print(name_list)
# ['apache', 'nginx', 'tomcat', 'redis', 'mysql']
################################ count 统计次数 ##################################
# def count(self, value):
# 统计元素出现的次数
name_list = ["apache","nginx","tomcat","redis","tomcat","tomcat"]
print(name_list.count("tomcat"))
# 3
################################# extend 扩展 ##################################
# def extend(self, iterable):
# 扩展列表 iterable (可迭代的) # 相当于批量添加
name_list = ["apache","nginx","tomcat","redis"]
num_list = [11,22,33,44]
name_list.extend(num_list)
print(name_list)
# ['apache', 'nginx', 'tomcat', 'redis', 11, 22, 33, 44]
################################# index 获取索引 ##################################
# def index(self, value, start=None, stop=None):
# 获取某个元素的索引
name_list = ["apache","nginx","tomcat","redis","hehe","nginx","redis","nginx"]
print(name_list.index("redis",4,7)) # 从下边1开始,到下标6结束,不包括下标6
# 6
################################ insert 插入 ##################################
# def insert(self, index, p_object):
# 表示往索引的具体位置插入数据
name_list = ["apache","nginx","tomcat","redis"]
name_list.insert(1,"mysql") # 先输入下标值,然后输入你要插入值。
print(name_list)
# ['apache', 'mysql', 'nginx', 'tomcat', 'redis']
################################ pop 移除 ##################################
# def pop(self, index=None):
# 默认是移除最后一个
name_list = ["apache","nginx","tomcat","redis"]
print(name_list.pop())
# redis
# 在原来列表中移除到最后一个元素,并赋值给aa
name_list = ["apache","nginx","tomcat","redis"]
aa = name_list.pop(2)
print(aa)
# redis
print(name_list)
# ['apache', 'nginx', 'tomcat']
# 可以使用下标值
name_list = ["apache","nginx","tomcat","redis"]
print(name_list.pop(2),11111111111) # pop中可以跟下标,移除的就是下标的值。
# tomcat 11111111111
################################ remove 移除 ##################################
# def remove(self, value):
# 移除某个元素 默认只移除第一个从左边匹配到的元素。
name_list = ["apache","nginx","tomcat","redis","apache","tomcat","apache"]
name2 = name_list.remove("apache") # 默认移除从左边移除的第一个apache
print(name_list)
# [ nginx', 'tomcat', 'redis', 'apache', 'tomcat', 'apache']
################################ reverse 反转 ##################################
# def reverse(self):
# 前后顺序倒过来
name_list = ["apache","nginx","tomcat","redis"]
print(name_list.reverse())
print(name_list)
# ['redis', 'tomcat', 'nginx', 'apache']
################################ sort 排序 ##################################
# def sort(self, cmp=None, key=None, reverse=False):
# 排序
name_list = ["apache","nginx","tomcat","redis","mysql"]
# 先不进行讲解,后期再调整更新
################################ del 删除指定位置的元素 ##################################
name_list = ["apache","nginx","tomcat","redis","mysql"]
del name_list[1] # 删除列表中下标为1的元素
print(name_list)
# ['apache', 'tomcat', 'redis', 'mysql']
name_list = ["apache","nginx","tomcat","redis","mysql"]
del name_list[1:3] #删除列表中下标1到3的元素
print(name_list)
# ['apache', 'redis', 'mysql']
#################################################################################
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:python入门怎么学习
下一篇:python反爬之懒加载
- python_字符串方法 2019-08-13
- python_公共方法 2019-08-13
- python 之 前端开发(HTTP协议、head标签、img标签、a标签、 2019-08-13
- python3 之 判断闰年小实例 2019-08-13
- 列表和元组 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash