python列表

xiaoxiao2021-02-28  97

1、列表是一种 可修改的集合类型,其元素可以是数字、string等基本类型,也可以是列表、元组、字典等集合对象,甚至可以是自定义的类型。其定义方式如下: >>> nums = [1,2,3,4]  >>> type(nums)  <type 'list'>  >>> print nums  [1, 2, 3, 4]  >>> strs = ["hello","world"]  >>> print strs  ['hello', 'world']  >>> lst = [1,"hello",False,nums,strs]  >>> type(lst)  <type 'list'>  >>> print lst  [1, 'hello', False, [1, 2, 3, 4], ['hello', 'world']]  2、用索引的方式访问列表元素, 索引从0开始,支持负数索引,-1为最后一个: >>> lst = [1,2,3,4,5]  >>> print lst[0]  1 >>> print lst[-1]  5 >>> print lst[-2]  4   3、可以使用变量来创建一个列表,但保存在列表中,只是变量中的内容的一个副本。即如果我们修改最初的变量,列表的值不会改变。   >>> fruit1 = 'apple' >>> f2 = 'bear' >>> f_list = [fruit1,f2] >>> f_list ['apple', 'bear'] >>> f2 = 'wall' >>> f_list ['apple', 'bear'] >>> f_list[1] = 'scc' >>> f_list ['apple', 'scc'] 3、列表中可以通过相关的函数可以获取有关的列表的信息 >>> color_list = ['red','blue','red','yellow'] >>> len(color_list) 4 >>> color_list.count('red') 2 >>> color_list.index('red') 0 >>> 'pink' in color_list False >>> 'red ' in color_list False >>> 'red' in color_list True 4、支持分片操作,可访问一个区间内的元素,支持不同的步长,可利用分片进行数据插入与复制操作 nums = [1,2,3,4,5]  print nums[0:3] #[1, 2, 3] #前三个元素     print nums[3:]  #[4, 5]  #后两个元素     print nums[-3:] #[3, 4, 5] #后三个元素 不支持nums[-3:0]     numsclone = nums[:]      print numsclone  #[1, 2, 3, 4, 5] 复制操作     print nums[0:4:2]  #[1, 3]  步长为2     nums[3:3] = ["three","four"]  #[1, 2, 3, 'three', 'four', 4, 5] 在3和4之间插入     nums[3:5] = []  #[1, 2, 3, 4, 5] 将第4和第5个元素替换为[] 即删除["three","four"]  5、支持加法和乘法操作 lst1 = ["hello","world"]  lst2 = ['good','time']  print lst1+lst2 #['hello', 'world', 'good', 'time']     print lst1*5 #['hello', 'world', 'hello', 'world', 'hello', 'world', 'hello', 'world', 'hello', 'world']  6、列表所支持的方法,可以用如下方式查看列表支持的公共方法: >>> [x for x in dir([]) if not x.startswith("__")]  ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']  def compare(x,y):    return 1 if x>y else -1   #【append】 在列表末尾插入元素  lst = [1,2,3,4,5]  lst.append(6)   print lst   #[1, 2, 3, 4, 5, 6]  lst.append("hello")  print lst   #[1, 2, 3, 4, 5, 6]     #【pop】 删除一个元素,并返回此元素的值 支持索引 默认为最后一个  x = lst.pop()  print x,lst   #hello [1, 2, 3, 4, 5, 6] #默认删除最后一个元素  x = lst.pop(0)  print x,lst   #1 [2, 3, 4, 5, 6] 删除第一个元素     #【count】 返回一个元素出现的次数  print lst.count(2)  #1       #【extend】 扩展列表 此方法与“+”操作的不同在于此方法改变原有列表,而“+”操作会产生一个新列表  lstextend = ["hello","world"]  lst.extend(lstextend)  print lst      #[2, 3, 4, 5, 6, 'hello', 'world'] 在lst的基础上扩展了lstextend进来      #【index】 返回某个值第一次出现的索引位置,如果未找到会抛出异常  print lst.index("hello") #5       #print lst.index("kitty") #ValueError: 'kitty' is not in list 出现异常    #【remove】 移除列表中的某个元素,如果待移除的项不存在,会抛出异常 无返回值  lst.remove("hello")  print lst   #[2, 3, 4, 5, 6, 'world'] "hello" 被移除     #lst.remove("kitty")     #ValueError: list.remove(x): x not in list     #【reverse】 意为反转 没错 就是将列表元素倒序排列,无返回值  print lst    #[2, 3, 4, 5, 6, 'world']  lst.reverse()   print lst    #[2, 3, 4, 5, 6, 'world']    #【sort】 排序  print lst  #由于上面的反转 目前排序为 ['world', 6, 5, 4, 3, 2]  lst.sort()   print lst  #排序后 [2, 3, 4, 5, 6, 'world']     nums = [10,5,4,2,3]  print nums   #[10,5,4,2,3]  nums.sort(compare)  print nums   #[2, 3, 4, 5, 10]       7、列表转换为迭代器。 所谓的迭代器就是具有next方法(这个方法在调用时不需要任何参数)的对象。在调用next方法时,迭代器会返回它的下一个值。如果next方法被调用,但迭代器没有值可以返回,就会引发一个StopIteration异常。迭代器相对于列表的优势在于,使用迭代器不必一次性将列表加入内存,而可以依次访问列表的数据。 依然用上面的方法查看迭代器的公共方法: lst = [1,2,3,4,5]  lstiter = iter(lst)  print [x for x in dir(numiter) if not x.startswith("__")]  >>>['next']    没错,只有next一个方法,对于一个迭代器,可以这样操作: lst = [1,2,3,4,5]  lstiter = iter(lst)     for i in range(len(lst)):    print lstiter.next() #依次打印    1   2   3   4   5  
转载请注明原文地址: https://www.6miu.com/read-42178.html

最新回复(0)