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

python-list:列表

时间:2017-08-07 20:47:02      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:删除   inpu   第一个   赋值   pyc   --   ==   str   list   

“列表”是一个值,它包含多个字构成的序列。术语“列表值”指的是列表本身(它作为一个值,可以保存在变量中、传递给函数)

list [‘aa‘,‘bb‘,‘cc‘,‘dd‘]是一个简单的列表

1.用列表下标取值

  list =[‘aa‘,‘bb‘,‘cc‘,‘dd‘]

  list[0]--aa

  list[1]--bb

  list[-1]--dd

2.切片取值(从列表中取得一个或多个值,返回结果是列表)

  list =[‘aa‘,‘bb‘,‘cc‘,‘dd‘]

  list[:3]--[‘aa‘,‘bb‘,‘cc‘]  #顾头不顾尾 

  list[:1]--[‘aa‘]

  list[0:-1]--[‘aa‘,‘bb‘,‘cc‘]

3.用len( )获取列表的长度

  list [‘aa‘,‘bb‘,‘cc‘,‘dd‘]

  len(list)--4

4.用下标改变列表中的值

  list =[‘aa‘,‘bb‘,‘cc‘,‘dd‘]

  list[0]=‘AA‘ -- [‘AA‘,‘bb‘,‘cc‘,‘dd‘]

  list[0]=list[1]---- [‘bb‘,‘bb‘,‘cc‘,‘dd‘]

5.列表连接和列表复制

  list= [‘aa‘,‘bb‘,‘cc‘,‘dd‘]

  num=[‘11‘,‘22‘,‘33‘,‘44‘]

  list+num--[‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘, ‘11‘, ‘22‘, ‘33‘, ‘44‘]

  list+[‘AA‘,‘BB‘]---[‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘,‘AA‘,‘BB‘]

  list*2--[‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘ ,‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘]

6.用del从列表中删除值

  list= [‘aa‘,‘bb‘,‘cc‘,‘dd‘]

  del list[0]-- [‘bb‘,‘cc‘,‘dd‘]

7.使用列表

cataNames = []
while True:
print (‘enter the name of cat‘+str(len(cataNames)) + ‘(or enter nothing to stop.):‘ )
name = str(input())
if name ==‘‘:
break
cataNames = cataNames+[name]
print (‘the cat names are:‘)
for name in cataNames:
print (‘‘+name)

C:\Python27\python.exe C:/Users/xiu/PycharmProjects/H5/day1/22.py
enter the name of cat0(or enter nothing to stop.):
‘cat01‘
enter the name of cat1(or enter nothing to stop.):
‘cat02‘
enter the name of cat2(or enter nothing to stop.):
‘cat03‘
enter the name of cat3(or enter nothing to stop.):
‘ ‘
the cat names are:
cat01
cat02
cat03

Process finished with exit code 0

 列表用于循环

list = [‘cat001‘,‘cat002‘,‘cat003‘]
for i in range(len(list)):
print (‘index ‘ + str(i)+ ‘in list is:‘+ list[i])

C:\Python27\python.exe C:/Users/xiu/PycharmProjects/H5/day1/22.py
index 0in list is:cat001
index 1in list is:cat002
index 2in list is:cat003

 

8.in和not in操作符

  list= [‘aa‘,‘bb‘,‘cc‘,‘dd‘]

  print ‘aa‘ in list--True

  print ‘aa1‘ in list--False


9.对变量增强的赋值操作(+、-、*、/、%)
  aa = 10
  aa +=2 #aa = aa +2 输出12
  aa -=2 #aa = aa-2 输出10
  aa *=2 #aa = aa *2 输出20
  aa /=2 #aa = aa/2 输出10
  aa %=2 #aa = aa % 2 输出0
10.用index()方法在列表中查找值:根据元素值查元素下标
  list= [‘aa‘,‘bb‘,‘cc‘,‘dd‘,‘cc‘]
  print list.index(‘bb‘) #1
  print list.index(‘cc‘) #2:若列表有多个相同元素值,则返回第一个

11.用append()/insert()方法在列表中添加值
  list= [‘aa‘,‘bb‘,‘cc‘,‘dd‘]
  list.append(‘ee‘) #[‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘, ‘ee‘]
  list.insert(0,‘AA‘) #[‘AA‘, ‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘, ‘ee‘]

12.remove()删除列表中的值
  list= [‘aa‘,‘bb‘,‘cc‘,‘dd‘]
  list.remove(‘aa‘) #[‘bb‘, ‘cc‘, ‘dd‘] 直接删元素值
  del list[0] #[‘cc‘, ‘dd‘] 通过下标删元素值

13.用sort()方法将列表中的值排序(只能对纯数字、纯字母的列表进行排序)
  list= [‘dd‘,‘cc‘,‘bb‘,‘aa‘]
  list.sort() #[‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘] 默认升序排列
  list.sort(reverse=True) #[‘dd‘, ‘cc‘, ‘bb‘, ‘aa‘]
  list.sort(reverse=False) #[‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘]
  list= [‘dd‘,‘cc‘,‘bb‘,‘aa‘,‘AA‘,‘BB‘,‘CC‘,‘DD‘]
  list.sort() #[‘AA‘, ‘BB‘, ‘CC‘, ‘DD‘, ‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘] 使用‘ASCII字符排序’,因此大写字母会在小写字母之前

  
  

 

  

python-list:列表

标签:删除   inpu   第一个   赋值   pyc   --   ==   str   list   

原文地址:http://www.cnblogs.com/ermm/p/7286465.html

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