ITPub博客

首页 > IT职业 > IT职场 > 列表方法

列表方法

IT职场 作者:zhaozy78 时间:2021-09-09 09:34:13 0 删除 编辑

1、list.append(obj)在列表末尾添加新的对象,且一次只添加一个元素。

>>> list = [123, 'kobego', [1, 2, 3], (1, 2)]>>> list.append([24, 23])>>> list[123, 'kobego', [1, 2, 3], (1, 2), [24, 23]]

2、list.extend(seq)在列表末尾可一次性添加多个列表元素。

>>> list = [123, 'kobego', [1, 2, 3], (1, 2)]>>> list.extend([24, 23])>>> list[123, 'kobego', [1, 2, 3], (1, 2), 24, 23]

3、list.pop(obj=list[-1])移除列表中的某一个元素(默认为末尾元素),且返回该移除元素的值。

>>> list = [123, 'kobego', [1, 2, 3], (1, 2)]>>> list.pop()(1, 2)>>> list[123, 'kobego', [1, 2, 3]]

4、list.remove(obj)移除列表中某一个元素的第一个匹配项,没有返回值。

>>> list = [123, 'kobego', 123, [1, 2, 3], (1, 2)]>>> list.remove(123)>>> list['kobego', 123, [1, 2, 3], (1, 2)]

5、list.index(obj)从列表中找出某个值第一个匹配项的索引位置,未找到则会报错。

>>> list = [123, 'kobego', 123, [1, 2, 3], (1, 2)]>>> list.index(123)0

6、list.insert(index, obj)将指定对象插入列表的指定位置。

>>> list = [123, 'kobego', 123, [1, 2, 3], (1, 2)]>>> list.insert(1, 'kobego24')>>> list[123, 'kobego24', 'kobego', 123, [1, 2, 3], (1, 2)]

7、list.reverse()对列表的元素进行反向排序。

>>> list = [123, 'kobego', 123, [1, 2, 3], (1, 2)]>>> list.reverse()>>> list[(1, 2), [1, 2, 3], 123, 'kobego', 123]

8、list.sort([func])对原列表进行排序。

>>> list = ['james', 'kobego', 'curry']>>> list.sort()>>> list['curry', 'james', 'kobego']

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3276/viewspace-2804809/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-14

  • 博文量
    72
  • 访问量
    426