MSCBSC 移动大发棋牌龙虎
搜索
登录注册
网络优化工程师招聘专栏 4G/LTE通信工程师最新职位列表 通信实习生/应届生招聘职位
 

  • 阅读:2728
  • 回复:0
PYTHON学习笔记【列表】
细阿哥
初级会员
鎵嬫満鍙风爜宸查獙璇? style=


 发短消息    关注Ta 

积分 480
帖子 96
威望 759 个
礼品券 15 个
专家指数 0
注册 2009-2-25
专业方向  通信
回答问题数 1
回答被采纳数 0
回答采纳率 0%
 
发表于 2019-10-10 22:16:15  只看楼主 

1     列表

python内置的一种数据类型是列表:listLIST是一种 有序的集合,可以随时添加和删除其中的元素,列表里面可以是数字或字符,或混合等。比如,列出班里所有同学的名字,可以用一个LIST表示

 

In [87]: a=["zhangs","lis","wangw","dada"]        #定义一个列表                         

 

In [88]: len(a)                               #获取列表长度,元素的个数

Out[88]: 4

 

In [89]: a[0]                                 #取第一个元素                             

Out[89]: 'zhangs'

 

In [92]: a[-1]                              #取倒数第一个元素                               

Out[92]: 'dada'

 

In [90]: a[4]                        #超出长度时报错  IndexError: list index out of range                          

---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

<ipython-input-90-ed40d1f49c42> in <module>

----> 1 a[4]

 

IndexError: list index out of range

 

1.      列表的相关操作

 

添加元素(增append,extend,insert

Ø  append在列表最后追加

 

In [97]: a                                                                 

Out[97]: ['zhangs', 'lis', 'wangw', 'dada']

 

In [98]: a.append('lufei')                                                 

 

In [99]: a                                                                  

Out[99]: ['zhangs', 'lis', 'wangw', 'dada', 'lufei']

 

Ø  extend两个列表的合并

 

In [100]: b                                                                

Out[100]: ['a', 'b', 1, 2, 3]

 

In [101]: a.extend(b)                                                       

 

In [102]: a                                                                

Out[102]: ['zhangs', 'lis', 'wangw', 'dada', 'lufei', 'a', 'b', 1, 2, 3]

 

Ø  insert 在任意位置插入

 

In [102]: a                                                                

Out[102]: ['zhangs', 'lis', 'wangw', 'dada', 'lufei', 'a', 'b', 1, 2, 3]

 

In [103]: a.insert(0,"wuxi")                                               

 

In [104]: a                                                                 

Out[104]: ['wuxi', 'zhangs', 'lis', 'wangw', 'dada', 'lufei', 'a', 'b', 1, 2, 3]

 

In [105]: a.insert(2,"haor")                                               

 

In [106]: a                                                                 

Out[106]: ['wuxi', 'zhangs', 'haor', 'lis', 'wangw', 'dada', 'lufei', 'a', 'b', 1, 2, 3]

 

修改元素

修改元素的时候,要通过下标来确定要修改的是哪个元素

 

In [107]: a[2]='haha'                                                      

 

In [108]: a                                                                 

Out[108]: ['wuxi', 'zhangs', 'haha', 'lis', 'wangw', 'dada', 'lufei', 'a', 'b', 1, 2, 3]

 

查找元素(in,not in,index,count)

 

in,not inin查找某个元素是否在列表里面,如果存在那么结果为True,否则为false

              not in in相反,如果不存在那第返回true,否则false

 

In [120]: "wangw" in a      

 

Out[120]: True

 

index返回某个元素的下标位,count统计元素在列表出现几次

 

In [117]: a.append("zhangs")

 

In [119]: a.count("zhangs")                                                

Out[119]: 2

 

 

In [128]: a=['a','b','c','a','b']                                          

 

In [130]: a.index('a',1,3)   #查找字符出现的下标,从下标1开始到下标3,查找不到报错

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-130-c884d924187e> in <module>

----> 1 a.index('a',1,3)

 

ValueError: 'a' is not in list

 

In [131]: a.index('a',1,4)                                                 

Out[131]: 3

 

In [132]: a.count("b")                                                      

Out[132]: 2

 

In [133]: a.count("d")                                                     

Out[133]: 0

 

删除元素(del不属于列表的函数,popremove

Ø  del 根据下标进行删除,也可以删除所有变量

Ø  pop默认删除最后一个元素

Ø  remove根据元素的值进行删除第一个

 

[del]

In [135]: a                                                                

Out[135]: ['a', 'b', 'c', 'a', 'b']

 

In [136]: b                                                                

Out[136]: ['a', 'b', 1, 2, 3]

 

In [138]: del b                #直接删除列表                                            

 

In [139]: b                                                                

---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-139-89e6c98d9288> in <module>

----> 1 b

 

NameError: name 'b' is not defined

 

In [140]: del a[2]            #删除列表某个元素                                             

 

In [141]: a                                                                

Out[141]: ['a', 'b', 'a', 'b']

 

[pop]

 

In [143]: a.pop()                                                          

Out[143]: 'b'

 

In [144]: a                                                                 

Out[144]: ['a', 'b', 'a']

 

[remove]

 

In [148]: a=['a','b','c','d','e','f','g','g','f','e']                      

 

In [149]: a.remove('e')                                                    

 

In [150]: a                                                                 

Out[150]: ['a', 'b', 'c', 'd', 'f', 'g', 'g', 'f', 'e']

 

排序sort,reverse

sort方法是将list按特定顺序重新排列,默认由小到大,参数reverse=true可改为倒序。reverse方法是将list逆置

 

[reverse]

In [152]: a                                                                

Out[152]: [2, 54, 7, 1, 4, 3, 5]

 

In [153]: a.reverse()                                                      

 

In [154]: a                                                                

Out[154]: [5, 3, 4, 1, 7, 54, 2]

 

[sort]

In [155]: a.sort()                                                         

 

In [156]: a                                                                 

Out[156]: [1, 2, 3, 4, 5, 7, 54]

 

In [163]: a.sort(reverse=True)                                             

 

In [164]: a                                                                

Out[164]: [54, 7, 5, 4, 3, 2, 1]


微信扫描分享本文到朋友圈
微信扫描二维码,手机阅读更便捷
每天分享朋友圈可获得威望奖励(前两次)

对本帖内容的看法? 我要点评

 
[充值威望,立即自动到帐] [VIP贵宾权限+威望套餐] 另有大量优惠赠送活动,请光临充值中心
充值拥有大量的威望和最高的下载权限,下载站内资料无忧

快速回复主题    
标题
内容
 上传资料请点左侧【添加附件】



当前时区 GMT+8, 现在时间是 2019-11-16 00:14:23
渝ICP备11001752号  Copyright @ 2006-2016 mscbsc.com  本站统一服务邮箱:mscbsc@163.com

Processed in 0.074382 second(s), 13 queries , Gzip enabled
TOP
清除 Cookies - 联系我们 - 移动通信网 - 移动大发棋牌龙虎 - 通信招聘网 - Archiver