• python列表部分源码分析


    1 def append(self, p_object): # real signature unknown; restored from __doc__
    2         """ L.append(object) -- append object to end """
    3         pass
    #1、练习(添加元素):
    li = [11,22,33,"aabc","cddg",234,"alex",]
    print(li)
    aa = li.append(78)    #在列表后添加列表元素,注意一次只能添加一个元素,否则报错
    print(li)
    #执行结果如下:
    [11,22,33,"aabc","cddg",234,"alex",]
    [11, 22, 33, 'aabc', 'cddg', 234, 'alex', 78]
    1 def count(self, value): # real signature unknown; restored from __doc__
    2         """ L.count(value) -> integer -- return number of occurrences of value """
    3         return 0
    1 #2、练习(计算列表元素出现个数)
    2 li = [11,22,33,"aabc","cddg",33,"alex",]
    3 aa = li.count(33)    #计算列表中指定元素出现的个数,一次只能计算一个元素
    4 print(aa)
    5 #执行结果如下:
    6 2
    1 def extend(self, iterable): # real signature unknown; restored from __doc__
    2         """ L.extend(iterable) -- extend list by appending elements from the iterable """
    3         pass
    1 #3、练习(列表扩展(向后))
    2 li = [11,22,33,"aabc","cddg",33,"alex",]
    3 li.extend("kkk")    #在列表后扩展,一次只能计算一个元素,字符串会拆分一个个显示
    4 print(li)
    5 li.extend(1234)     #如果扩展整型数字会报错,数字不可拆分
    6 print(li)
    7 #执行结果:
    8 [11, 22, 33, 'aabc', 'cddg', 33, 'alex', 'k', 'k', 'k']
    9 报错:TypeError: 'int' object is not iterable
    1 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
    2         """
    3         L.index(value, [start, [stop]]) -> integer -- return first index of value.
    4         Raises ValueError if the value is not present.
    5         """
    6         return 0
    1 #4、练习(找出列表元素索引位置)
    2 li = [11,22,33,"aabc","cddg",33,"alex",]
    3 aa = li.index(22)
    4 print(aa)
    5 bb = li.index(33)
    6 print(bb)     #查找列表中元素的索引,重复存在则输出靠前的索引位置
    7 #执行结果:
    8 1
    9 2
    1 def insert(self, index, p_object): # real signature unknown; restored from __doc__
    2         """ L.insert(index, object) -- insert object before index """
    3         pass
    1 #5、练习(插入元素)
    2 li = [11,22,33,"aabc","cddg",33,"alex",]
    3 li.insert(1,"kkk")       #根据列表下标位置,直接插入
    4 print(li)
    5 #执行结果:
    6 [11, 'kkk', 22, 33, 'aabc', 'cddg', 33, 'alex']
    1 def pop(self, index=None): # real signature unknown; restored from __doc__
    2         """
    3         L.pop([index]) -> item -- remove and return item at index (default last).
    4         Raises IndexError if list is empty or index is out of range.
    5         """
    6         pass
    1 #6、练习(取出元素)
    2 li = [11,22,33,"aabc","cddg",33,"alex",]
    3 li.pop(3)      #取出(删除)指定下标的元素
    4 print(li)
    5 li.pop()       #如果不输入下标,则默认取出最后一个元素,注意:超出下标范围会报错
    6 print(li)
    7 #执行结果:
    8 [11, 22, 33, 'cddg', 33, 'alex']
    9 [11, 22, 33, 'cddg', 33]
    1 def remove(self, value): # real signature unknown; restored from __doc__
    2         """
    3         L.remove(value) -- remove first occurrence of value.
    4         Raises ValueError if the value is not present.
    5         """
    6         pass
    1 #练习7、(移除元素)
    2 li = [11,22,33,"aabc","cddg",33,"alex",]
    3 li.remove(33)      #移除元素,如果有重复的,则移除靠前的那个
    4 print(li)
    5 #执行结果:
    6 [11, 22, 'aabc', 'cddg', 33, 'alex']
    1 def reverse(self): # real signature unknown; restored from __doc__
    2         """ L.reverse() -- reverse *IN PLACE* """
    3         pass
    1 练习8、(翻转列表)
    2 li = [11,22,33,"aabc","cddg",33,"alex",]
    3 li.reverse()    #翻转列表元素顺序
    4 print(li)
    5 #执行结果:
    6 ['alex', 33, 'cddg', 'aabc', 33, 22, 11]
    1 def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
    2         """
    3         L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    4         cmp(x, y) -> -1, 0, 1
    5         """
    6         pass
    #练习9、(列表重新排序)
    #li = [11,22,33,"aabc","cddg",33,"alex",]   报错
    # li.sort()
    # print(li)
    aa = ["kkk","bbb","jjj","www"]     #列表按照ASCII码,重新排序,注意:列表里的元素只能全是字符串,或者全是数字,否则会报错
    aa.sort()
    print(aa)
    #执行结果:
    ['bbb', 'jjj', 'kkk', 'www']
  • 相关阅读:
    模拟手机售电影票
    flex布局
    如何制作快速加载的HTML页面
    css布局列表,自适应
    iOS项目中常见定时器
    iOS中TableView的分割线顶格样式的实现
    iOS本地化项目上传到gitHub
    iOS初学者易懵逼的Timer延时
    iOS同种界面moda和push方式切换
    iOS之push和modal大不同
  • 原文地址:https://www.cnblogs.com/repo/p/5414709.html
Copyright © 2020-2023  润新知