Learning summary of Python list class

This is some code that I learned while typing before, and the notes of the code throughout the code look a bit messy...

#!/usr/bin/ebv python
#-*- coding:utf-8 -*-
from operator import itemgetter, attrgetter

class Student:
  def __init__(self, id,name, age):
    self.id = id
    self.name = name
    self.age = age
  def __repr__(self):
    return repr((self.name, self.id, self.age))


## 列表
if __name__ == "__main__":
  nl = [[5,'a'],[4,'b'],[6,'c'],[3,'d'],[7,'e'],10,[2,'abcdef'],\
        [8,'abcdef',0],[1,'',1,1,0],[9],[0,'a'],[0,'b'],[0,'a'],[10,'a']]
  print "1.计数:"
  print nl.count(5)   ## 计数,看总共有多少个5  --> 0
  print nl.count([0,'a'])  ## 计数,看总共有多少个[0,'a']  --> 2

  print "2.获取第一个匹配元素下标:"
  try : print nl.index(3)      ## 查询 nl 的第一个3的下标
  except :print "3 is not in list"    ## 由于列表没有3会报错,不try会crash。
  print nl.index(10)    ## 5

  print "3.向列表插入元素:"
  nl.append([100,'append'])      ## 在列表末尾插入[100,'append']  直接将[100,'append']整体插入
  print nl
  nl.extend([101,'extend1',[1,2]])      ## 将[101,'extend']中的元素追加到nl中,相当于连接了两个列表
  nl.extend((102, 'extend2'))
  nl.extend({'103':'extend2'})    ## 只会将'103'追加进去,
  nl.extend('abc')  ## 会将a b c分别插入,虽然python不分字符字符串,但是联想一下c语言中字符和字符串的关系吧
  ##nl.entend(123)   ## crash 。也就是说extend其实操作的是:列表、元祖、字典、字符串这类的数据
  print nl
  nl.insert(3,'insert')    ## 在下标为3的地方插入'insert'
  print nl

  print "4.排序:"
  nl.sort()    ## 自动递归型排序,先按nl中元素第一位排序,依次往后
  print nl
  ### from operator import itemgetter, attrgetter
  nl = [[5,'e'],[4,'f'],[6,'d'],[3,'g'],[7,'c'],[2,'h'],'0abc',[8,'b'],[1,'i'],[9,'a'],[10,''],[10,''],[0,'j']]
  print sorted(nl,key=itemgetter(0))   ## 以元素中下标为0的位排序
  print sorted(nl, key=itemgetter(1))  ## 以元素中下标为1的位排序
  students = [
    Student(3,'zhangsan',21),
    Student(4,'lisi',24),
    Student(2,'wangchao',30),
    Student(5,'mahan',19),
    Student(1,'xiaoming',25),
]
  print sorted(students, key=attrgetter('id'))  ## 以元素id为排序依据
  ## 关于sort sorted还有很多,可以参考:https://www.cnblogs.com/whaben/p/6495702.html

  print "5.从列表删除元素:"
  print "list:",nl
  print "pop():",nl.pop()     ## 删除列表末尾元素
  print "pop(2)",nl.pop(2)    ## 删除列表下标为2的元素 默认-1
  print "new list:",nl
  nl.remove([10,''])            # 从nl中去除第一个[10,'']  ## 可以与count结合,删除所有目标元素
  print "remove([10,''])",nl
  ## 其他:
  ##+, -, >, <, 以及下标引用[start:end]等等,从根本上都是定义在类内部的方法
  ## 有一点注意:+操作和extend有事可以达到同样效果, 但是用+,原list不变,有返回值,
  ## extend只在原列表追加,说明+定义了新变量,开辟了新的内存空间,尽量不要用。
  1.计数:
0
2
2.获取第一个匹配元素下标:
3 is not in list
5
3.向列表插入元素:
[[5, 'a'], [4, 'b'], [6, 'c'], [3, 'd'], [7, 'e'], 10, [2, 'abcdef'], [8, 'abcdef', 0], [1, '', 1, 1, 0], [9], [0, 'a'], [0, 'b'], [0, 'a'], [10, 'a'], [100, 'append']]
[[5, 'a'], [4, 'b'], [6, 'c'], [3, 'd'], [7, 'e'], 10, [2, 'abcdef'], [8, 'abcdef', 0], [1, '', 1, 1, 0], [9], [0, 'a'], [0, 'b'], [0, 'a'], [10, 'a'], [100, 'append'], 101, 'extend1', [1, 2], 102, 'extend2', '103', 'a', 'b', 'c']
[[5, 'a'], [4, 'b'], [6, 'c'], 'insert', [3, 'd'], [7, 'e'], 10, [2, 'abcdef'], [8, 'abcdef', 0], [1, '', 1, 1, 0], [9], [0, 'a'], [0, 'b'], [0, 'a'], [10, 'a'], [100, 'append'], 101, 'extend1', [1, 2], 102, 'extend2', '103', 'a', 'b', 'c']
4.排序:
[10, 101, 102, [0, 'a'], [0, 'a'], [0, 'b'], [1, 2], [1, '', 1, 1, 0], [2, 'abcdef'], [3, 'd'], [4, 'b'], [5, 'a'], [6, 'c'], [7, 'e'], [8, 'abcdef', 0], [9], [10, 'a'], [100, 'append'], '103', 'a', 'b', 'c', 'extend1', 'extend2', 'insert']
[[0, 'j'], [1, 'i'], [2, 'h'], [3, 'g'], [4, 'f'], [5, 'e'], [6, 'd'], [7, 'c'], [8, 'b'], [9, 'a'], [10, ''], [10, ''], '0abc']
[[10, ''], [10, ''], '0abc', [9, 'a'], [8, 'b'], [7, 'c'], [6, 'd'], [5, 'e'], [4, 'f'], [3, 'g'], [2, 'h'], [1, 'i'], [0, 'j']]
[('xiaoming', 1, 25), ('wangchao', 2, 30), ('zhangsan', 3, 21), ('lisi', 4, 24), ('mahan', 5, 19)]
5.从列表删除元素:
list: [[5, 'e'], [4, 'f'], [6, 'd'], [3, 'g'], [7, 'c'], [2, 'h'], '0abc', [8, 'b'], [1, 'i'], [9, 'a'], [10, ''], [10, ''], [0, 'j']]
pop(): [0, 'j']
pop(2) [6, 'd']
new list: [[5, 'e'], [4, 'f'], [3, 'g'], [7, 'c'], [2, 'h'], '0abc', [8, 'b'], [1, 'i'], [9, 'a'], [10, ''], [10, '']]
remove([10,'']) [[5, 'e'], [4, 'f'], [3, 'g'], [7, 'c'], [2, 'h'], '0abc', [8, 'b'], [1, 'i'], [9, 'a'], [10, '']]

进程完成,退出码 0

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325868247&siteId=291194637