码迷,mamicode.com
首页 > 编程语言 > 详细

python中列表的应用

时间:2016-08-02 08:50:35      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

本文主要介绍了: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

运行结果:
技术分享

 




python中列表的应用

标签:

原文地址:http://www.cnblogs.com/pythonlh/p/5727946.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!