标签:
本文主要介绍了:python中列表的主要应用和一些列表自带的一些函数
代码:
#!/usr/bin/env python
# author by lh
# -*- coding:utf-8 -*-
name_list=[‘al‘,‘ed‘,‘fg‘]
print name_list #打印列表
print name_list[0] #索引
print name_list[0:2] #切片
for i in name_list: #for循环打印
print i
name_list.append(‘ed‘) #添加
print name_list
i=name_list.count(‘ed‘) #统计相同的字符有几个
print i
temp=[‘fg‘,‘fg‘,‘fg‘]
name_list.extend(temp) #批量添加
print name_list
print name_list.index(‘al‘)
#获取指定字符串的索引位置
name_list.insert(1,‘sx‘)
print name_list
a1=name_list.pop() #移除最后一个
print a1
name_list.remove(‘ed‘) #移除想要删除的字符串(如果有相同的,默认移除从左到右第一个)
print name_list
name_list.reverse() #将字符串反过来打印(反转)
print name_list
del name_list[0:4] #删除指定位置的字符串(索引和切片都适用)
print name_list
运行结果:
标签:
原文地址:http://www.cnblogs.com/pythonlh/p/5727946.html