# #!/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']
#################################################################################