python的学习之路(二)——列表的常用模块

1、len() 得出列表中有多少数据项;

student = ["zhangsan", "lisi", "wangwu", "zhangergou"]
print(len(student))
print(student[0])
print(student[3])

4
zhangsan
zhangergou

2、append() 在列表末尾追加一个数据项

def append(self, *args, **kwargs): # real signature unknown
    """
    Append a single item to the end of the bytearray.    #在bytearray的末尾追加一个项。
    
      item
        The item to be appended.
    """
    pass
student.append("liudehua")
print(student)

['zhangsan', 'lisi', 'wangwu', 'zhangergou', 'liudehua']

3、extend() 在列表末尾增加一个数据项集合;

def extend(self, *args, **kwargs): # real signature unknown
    """
    Append all the items from the iterator or sequence to the end of the bytearray. #将迭代器或序列中的所有项添加到bytearray的末尾
    
      iterable_of_ints
        The iterable of items to append.
    """
    pass
student.extend(["guofucheng", "chenglong"])
print(student)

['zhangsan', 'lisi', 'wangwu', 'zhangergou', 'liudehua', 'guofucheng', 'chenglong']

*append与extend区别:

append:只是加入一项,加入集合也只是为列表的最后一项,他的序列值固定

extend:加入一个数据项集合

append:

data = [1, 2, 3]
data.append([4, 5])
print(data)

[1, 2, 3, [4, 5]]

extend:

data = [1, 2, 3]
data.extend([6, 7])
print(data)

[1, 2, 3, 6, 7]

4、pop() 在列表末尾删除一个数据项;

def pop(self, *args, **kwargs): # real signature unknown
    """
    Remove and return a single item from B. #从B中移除并返回一个项。
    
      index
        The index from where to remove the item.
        -1 (the default value) means remove the last item.
    
    If no index argument is given, will pop the last item.
    """
    pass
student.pop()
print(student)

['zhangsan', 'lisi', 'wangwu', 'zhangergou', 'liudehua', 'guofucheng']

5、remove() 在列表中删除一个特定的数据项

student.remove("guofucheng")
print(student)
student.remove(student[0])
print(student)

['zhangsan', 'lisi', 'wangwu', 'zhangergou', 'liudehua']
['lisi', 'wangwu', 'zhangergou', 'liudehua']

6、insert() 在特定位置前面增加一个数据项;

student.insert(0,"liangliang")
print(student)
student.insert(2,"liuneng")
print(student)

['liangliang', 'lisi', 'wangwu', 'zhangergou', 'liudehua']
['liangliang', 'lisi', 'liuneng', 'wangwu', 'zhangergou', 'liudehua']

7、count() 统计某个数据项在列表中出现的次数

student.count("liangliang")

1

*8、copy()将数据类型进行复制

deepcopy() 我们寻常意义上的复制,复制后作为一个独立的个体存在,改变复制后的值对原来没有影响。

import copy
data = [1,2, [3,4]]
data2 = copy.deepcopy(data)
data2 [2][0] = 1
print(data)
print(data2)

[1, 2, [3, 4]]
[1, 2, [1, 4]]

copy()只复制第一层,将原来的数据打上标签,当其中一个标签变化是,两个数据都会发生改变。

import copy
data = [1,2, [3,4]]
data1 = data.copy()
data1 [2][0] = 1
print(data)
print(data1)

[1, 2, [1, 4]]
[1, 2, [1, 4]]

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值