首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

从0到1/Python之列表

Python学习——列表

【1】#列表的表示:

列表中用方括号[ ]表示,元素之间用逗号,隔开。

>>>property=['name','gender','weight','height']

>>>property

['name','gender', 'weight', 'height']

【2】#元素索引:

>>>property[0]

'name'

【3】#列表中第一个元素标号,依次累加;此外,最后可以通过-1索引最后一个元素,方向依次累减:

例1:

>>>property=['name','gender','weight','height']

>>>print(property[0]+'\n'+property[1]+'\n'+property[2]+'\n'+property[3])

name

gender

weight

height

例2:

>>>property=['name','gender','weight','height']

>>>print(property[-1]+'\n'+property[-2]+'\n'+property[-3]+'\n'+property[-4])

height

weight

gender

name

【4】#修改列表元素,修改列表下表并赋值:

>>>property=['name','gender','weight','height']

>>>print(property)

['name','gender', 'weight', 'height']

>>>property[1]='boy'

>>>print(property)

['name','boy', 'weight', 'height']

【5】#在列表末尾添加新元素append(‘元素’):

例1:

>>>property=['name','gender','weight','height']

>>>print(property)

['name','gender', 'weight', 'height']

>>>property.append('new_factor')

>>>print(property)

['name','gender', 'weight', 'height', 'new_factor']

例2:

>>>property_empty=[]

>>>property_empty.append('one')

>>>property_empty.append('two')

>>>property_empty.append('three')

>>>print(property_empty)

['one','two', 'three']

【6】#在列表中插入元素insert(位置,’新元素’):

>>>property=['name','gender','weight','height']

>>>print(property)

['name','gender', 'weight', 'height']

>>>property.insert(1,'new_factor')

>>>print(property)

['name','new_factor', 'gender', 'weight', 'height']

【7】#列表中删除元素del列表[位置]

>>>property=['name','gender','weight','height']

>>>print(property)

['name','gender', 'weight', 'height']

>>>del property[0]

>>>print(property)

['gender','weight', 'height']

【8】#使用pop(位置),删除元素(即:从列表中弹出元素)

>>>property=['name','gender','weight','height']

>>>print(property)

['name', 'gender','weight', 'height']

>>>tan_chu=property.pop(2)

>>>print(property)

['name','gender', 'height']

>>>print(tan_chu)

Weight

【9】#使用remove(‘元素’),删除列表中指定的元素:

例1:

>>>property=['name','gender','weight','height']

>>>remove_factor=property[1]

>>>property.remove(remove_factor)

>>>print(property)

['name','weight', 'height']

>>>print(remove_factor)

Gender

例2:

>>>property=['name','gender','weight','height']

>>>remove_factor='gender'

>>>property.remove(remove_factor)

>>>print(property)

['name', 'weight','height']

【10】#sort()函数,对列表实现永久排序:

例1,正向排序.sort():

>>>ex_list=['bill','alice','calen','eva','demen']

>>>print(ex_list)

['bill','alice', 'calen', 'eva', 'demen']

>>>ex_list.sort()

>>>print(ex_list)

['alice','bill', 'calen', 'demen', 'eva']

例2,反向排序.sort(reverse=True)、(注:True首字母注意大小写):

>>>ex_list=['bill','alice','calen','eva','demen']

>>>print(ex_list)

['bill','alice', 'calen', 'eva', 'demen']

>>>ex_list.sort(reverse=True)

>>>print(ex_list)

['eva','demen', 'calen', 'bill', 'alice']

【11】#sorted()函数,对列表实现临时排序:

例1:

>>>ex_list=['bill','alice','calen','eva','demen']

>>>temporary=sorted(ex_list)

>>>print(temporary)

['alice','bill', 'calen', 'demen', 'eva']

>>>print(sorted(ex_list))

['alice','bill', 'calen', 'demen', 'eva']

>>>print(ex_list)

['bill','alice', 'calen', 'eva', 'demen']

例2,正反序:

>>>ex_list=['bill','alice','calen','eva','demen']

>>>temporary_positive=sorted(ex_list)

>>>temporary_passive=sorted(ex_list,reverse=True)

>>>print(ex_list,'\n',temporary,'\n',temporary_positive)

['bill','alice', 'calen', 'eva', 'demen']

['eva', 'demen', 'calen', 'bill', 'alice']

['alice', 'bill', 'calen', 'demen', 'eva']

【12】#reverse()函数,颠倒列表元素顺序:

>>>ex_list=['bill','alice','calen','eva','demen']

>>>print(ex_list)

['bill','alice', 'calen', 'eva', 'demen']

>>>ex_list.reverse()

>>>copy_1=ex_list

>>>print('\n',ex_list,'\n',copy_1)

['demen', 'eva', 'calen', 'alice', 'bill']

['demen', 'eva', 'calen', 'alice', 'bill']

【13】#len(列表),确定列表长度:

>>>ex_list=['bill','alice','calen','eva','demen']

>>>len_list=len(ex_list)

>>>print('\t',len_list)

5

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181220G0STZH00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券