学习D4

判断指定的元素是否在当前的列表中

用 in 和 not in

list1= [1,2,3]

10 in list1

返回 false

列表数据修改

list1[0] = 0

下标不能越界

列表数据的操作

append 方法

list.append(object) 向列表中添加一个对象object

将所指定的object追加在末尾

只追加一个

extend方法

list.extend(sequence) 把一个序列seq的内容添加到列表中 参数必须是可迭代的

会将元素全取出来追加在末尾

index方法

index 查询值的索引

def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
    """
    B.index(sub[, start[, end]]) -> int
    
    Return the lowest index in B where subsection sub is found,
    such that sub is contained within B[start,end].  Optional
    arguments start and end are interpreted as in slice notation.
    
    Raises ValueError when the subsection is not found.
    """
    return 0
insert方法

insert添加数据在指定的索引值前 会改变原始数据

def insert(self, *args, **kwargs): # real signature unknown
    """
    Insert a single item into the bytearray before the given index.
    
      index
        The index where the value is to be inserted.
      item
        The item to be inserted.
    """
    pass
列表数据删除 remove

空列表或超出索引就报错

def remove(self, *args, **kwargs): # real signature unknown
    """
    Remove the first occurrence of a value in the bytearray.
    
      value
        The value to remove.
    """
    pass
列表数据提取 pop

删除并返回提取出的值

如果没给索引值就提取最后一个

若索引值超出列表大小报错

def pop(self, *args, **kwargs): # real signature unknown
    """
    Remove and return a single item from 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 
clear方法

clear 清除列表中所有数据

def clear(self): # real signature unknown; restored from __doc__
    """ D.clear() -> None.  Remove all items from D. """
    pass
del方法

del 未指定下标就删除所有对象

用法

del list1

del list1[2]

列表转字符串

join方法

join参数要是可迭代对象并且是字符串

例1

list1 = [‘a’,‘b’,‘c’] 可以

str = ‘’.join(list1)

list1 = [1,2,3,4] 不行
例2
str = ‘’.join([str(i) for i in list1])

zip 方法 压缩

将相同下标的内容打包成元组

用for循环遍历

元组:不可修改的值

例:

list1 = [1,2,3,4,5,6,7,8]
list2 = [1,2,3,4,5,6,7,8]
str1 = zip(list1,list2)
for i in str1:
    print(i)
enumerate 方法 枚举函数

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

seasons = ['Spring', 'Summer', 'Fall', 'Winter']
 list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
 list(enumerate(seasons, start=1))       # 下标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

参数需要时可迭代对象

返回值是迭代器 前面是索引后面是值

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值