zoukankan      html  css  js  c++  java
  • 基础数据类型-list

    序列是python中的基础数据结构,序列里每一个元素都有一个下标,从0开始,依次递增.

    list,tuple,dictionary是使用最频繁的三类数据结构。

    (1)序列都有的方法包括:索引,切片,检查成员,加,乘:

     1 #!/usr/bin/env python
     2 # -*- coding: UTF-8 -*-
     3 #索引
     4 list_name = ['Paul', 'John', 'James']
     5 print(list_name[1])
     6 
     7 #切片
     8 list_number = [1, 1, 2, 3, 4, 5]
     9 print(list_number[1:5])
    10 
    11 #检查成员
    12 if 'Joshua' in list_name:
    13     print("Joshua is in list_name")
    14 else:
    15     print("Joshua is not in list_name")
    16 
    17 #
    18 print(list_name + list_number)
    19 
    20 #
    21 print(list_name * 2)
    Code
    1 John
    2 [1, 2, 3, 4]
    3 Joshua is not in list_name
    4 ['Paul', 'John', 'James', 1, 1, 2, 3, 4, 5]
    5 ['Paul', 'John', 'James', 'Paul', 'John', 'James']
    Result

    (2)遍历列表:

    1 #遍历列表
    2 for number in list_number:
    3     print(number)
    Code

    (3)list的函数有len(),max(),min(),list()

    1 #函数
    2 print(len(list_number))
    3 print(max(list_number))
    4 print(min(list_number))
    5 tuple_number = (1, 3, 5, 7)
    6 print(type(tuple_number), type(list(tuple_number)))#list()强制将序列转化为list类型
    Code
    1 6
    2 5
    3 1
    4 <class 'tuple'> <class 'list'>
    Result

    (4)list的常用方法有

    -append(), extend(),insert();

    1 #append(), extend(),insert();
    2 list_number = [1, 1, 2, 3, 4, 5]
    3 list_number.append(7) #在列表末尾添加新的对象
    4 print(list_number)
    5 list_number.extend([10, 11, 12]) #在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
    6 print(list_number)
    7 list_number.insert(1, 99) #list.insert(index, obj)将对象插入列表中index位置
    8 print(list_number)
    Code
    1 [1, 1, 2, 3, 4, 5, 7]
    2 [1, 1, 2, 3, 4, 5, 7, 10, 11, 12]
    3 [1, 99, 1, 2, 3, 4, 5, 7, 10, 11, 12]
    Result

    -remove(),pop(),clear();

    1 #remove(),pop(),clear();
    2 list_number = [1, 1, 2, 3, 4, 5]
    3 list_number.remove(1)#移除列表中某个值的第一个匹配项
    4 print(list_number)
    5 pop_number = list_number.pop(4)#list.pop(obj=list[-1]) 移除列表中的索引位置元素(默认最后一个元素),并且返回该元素的值
    6 print("pop number:", pop_number)
    7 print(list_number)
    8 list_number.clear()#清空列表
    9 print(list_number)
    Code
    1 [1, 2, 3, 4, 5]
    2 pop number: 5
    3 [1, 2, 3, 4]
    4 []
    Result

    -sort(),reverse();

    1 #sort(),reverse();
    2 list_number = [4, 2, 5, 7, 1, 3]
    3 print(list_number)
    4 list_number.sort() #对原列表进行排序
    5 print(list_number)
    6 list_number.reverse() #反向原列表中元素
    7 print(list_number)
    Code
    1 [4, 2, 5, 7, 1, 3]
    2 [1, 2, 3, 4, 5, 7]
    3 [7, 5, 4, 3, 2, 1]
    Result

    -count(),index(),copy()

    1 #count(),index(),copy()
    2 list_number = [1, 1, 2, 2, 2, 3, 3, 3, 3]
    3 print(list_number.count(3)) #统计某个元素在列表中出现的次数
    4 print(list_number.index(2)) #从列表中找出某个值第一个匹配项的索引位置
    5 print("address of list_number:", id(list_number))
    6 copy_list_number = list_number.copy() #复制列表
    7 print(copy_list_number)
    8 print("address of copy_list_number:", id(copy_list_number))
    Code
    1 4
    2 2
    3 address of list_number: 35406832
    4 [1, 1, 2, 2, 2, 3, 3, 3, 3]
    5 address of copy_list_number: 35421720
    Result

    最后来看下list类的定义:

     1 class list(object):
     2     """
     3     list() -> new empty list
     4     list(iterable) -> new list initialized from iterable's items
     5     """
     6     def append(self, p_object): # real signature unknown; restored from __doc__
     7         """ L.append(object) -- append object to end """
     8         pass
     9 
    10     def count(self, value): # real signature unknown; restored from __doc__
    11         """ L.count(value) -> integer -- return number of occurrences of value """
    12         return 0
    13 
    14     def extend(self, iterable): # real signature unknown; restored from __doc__
    15         """ L.extend(iterable) -- extend list by appending elements from the iterable """
    16         pass
    17 
    18     def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
    19         """
    20         L.index(value, [start, [stop]]) -> integer -- return first index of value.
    21         Raises ValueError if the value is not present.
    22         """
    23         return 0
    24 
    25     def insert(self, index, p_object): # real signature unknown; restored from __doc__
    26         """ L.insert(index, object) -- insert object before index """
    27         pass
    28 
    29     def pop(self, index=None): # real signature unknown; restored from __doc__
    30         """
    31         L.pop([index]) -> item -- remove and return item at index (default last).
    32         Raises IndexError if list is empty or index is out of range.
    33         """
    34         pass
    35 
    36     def remove(self, value): # real signature unknown; restored from __doc__
    37         """
    38         L.remove(value) -- remove first occurrence of value.
    39         Raises ValueError if the value is not present.
    40         """
    41         pass
    42 
    43     def reverse(self): # real signature unknown; restored from __doc__
    44         """ L.reverse() -- reverse *IN PLACE* """
    45         pass
    46 
    47     def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
    48         """
    49         L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    50         cmp(x, y) -> -1, 0, 1
    51         """
    52         pass
    Class List
  • 相关阅读:
    k8s默认调度器常见调度算法解析
    K8s集群相关证书
    flannel overlay网络浅析
    Pod挂载LocalStoragePv过程理解
    k8s开发实践
    Flex布局【弹性布局】学习
    python中的技巧——杂记
    Tarjan + bfs HYSBZ 1179Atm
    POJ1988 Cube stacking(非递归)
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/z-joshua/p/6305686.html
Copyright © 2011-2022 走看看