列表中的小方法

发布时间:2020-06-06 15:29:25 作者:v_fanyunxiao
来源:网络 阅读:339

本博文涉及到的方法有:index()、append()、insert()、remove()、sort()。

1.index():接受一个值,如果此值在列表中,就会返回它的下标;如果此值出现多次,只返回第一个下标

list = ['hello','world','hi']
print (list.index('hello'))
print (list.index('hi'))

返回值为:0,1

2.append()在列表的末尾插入数据,insert(新值下标,新值)表示在列表中任意位置插入数据。

        注意:append(),insert()的返回值都是None

list = ['hello','world','hi']
list.append('添加到末尾')
list.insert(10,'任意位置新值')

3.remove()接受一个值,并从列表中删除

list = ['hello','world','hi']
list.remove('hello')

4.sort()用于数值或者字符串列表的排序。字符串是按照首字母的ASCII值排序

list = ['hello','world','Hi','a','A']
list.sort()                    #默认升续排列
print (list)
list.sort(reverse = True)    #降续排列
print (list)
list.sort(key = str.lower)    #按照普通字典顺序(所有的项都认为小写)
print (list)


推荐阅读:
  1. python列表函数小例子
  2. Python中的列表以及列表的处理方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python列表 小方法

上一篇:如何离线部署k3s

下一篇:Oracle 11.2 单实例连接ASM时需要注意的事项以及问题处理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》