首页 > 编程语言 >Python List

Python List

时间:2023-06-09 16:25:12浏览次数:43  
标签:__ name Python List self list 列表 root

List 数据类型

一、创建一个列表

用把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:

>>> name_list = ["root", "gm", "hlr"]

二、访问列表中的值

使用下标索引来访问列表中的值,与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等。

>>> name_list
['root', 'gm', 'hlr']

三、查看列表可进行的操作

>>> help(name_list)
·················
>>> dir(name_list)
['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
append("STRING"):向列表的末尾追加一个新的值
clear():删除所有的元素
copy():浅 copy ,原始列表中包含可变对象(如嵌套列表或字典),则更改这些对象可能会在复制后的列表中反映出来
count("STRING"):统计某字符串或值在列表中出现的次数
extend(list2):将可迭代对象中的元素逐个添加到列表的末尾,扩展原始列表。可迭代对象:列表,元组,字符串
index("STRING"):某值在列表中第一次出现的位置
insert(N,"STRING"):向列表的位置N处插入一个新值
pop(obj=list[-1]):删除列表某个位置的值,默认为最后一个值
remove("STRING"):移除列表中某个值的第一个匹配项
reverse():列表元素值反序排列
sort():对列表内的所以值按照ASCII码进行排序
list 函数方法
class list(object):
    """
    Built-in mutable sequence.
    
    If no argument is given, the constructor creates a new empty list.
    The argument must be an iterable if specified.
    """
    def append(self, *args, **kwargs): # real signature unknown
        """ Append object to the end of the list. """
        pass

    def clear(self, *args, **kwargs): # real signature unknown
        """ Remove all items from list. """
        pass

    def copy(self, *args, **kwargs): # real signature unknown
        """ Return a shallow copy of the list. """
        pass

    def count(self, *args, **kwargs): # real signature unknown
        """ Return number of occurrences of value. """
        pass

    def extend(self, *args, **kwargs): # real signature unknown
        """ Extend list by appending elements from the iterable. """
        pass

    def index(self, *args, **kwargs): # real signature unknown
        """
        Return first index of value.
        
        Raises ValueError if the value is not present.
        """
        pass

    def insert(self, *args, **kwargs): # real signature unknown
        """ Insert object before index. """
        pass

    def pop(self, *args, **kwargs): # real signature unknown
        """
        Remove and return item at index (default last).
        
        Raises IndexError if list is empty or index is out of range.
        """
        pass

    def remove(self, *args, **kwargs): # real signature unknown
        """
        Remove first occurrence of value.
        
        Raises ValueError if the value is not present.
        """
        pass

    def reverse(self, *args, **kwargs): # real signature unknown
        """ Reverse *IN PLACE*. """
        pass

    def sort(self, *args, **kwargs): # real signature unknown
        """
        Sort the list in ascending order and return None.
        
        The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
        order of two equal elements is maintained).
        
        If a key function is given, apply it once to each list item and sort them,
        ascending or descending, according to their function values.
        
        The reverse flag can be set to sort in descending order.
        """
        pass

    def __add__(self, *args, **kwargs): # real signature unknown
        """ Return self+value. """
        pass

    def __class_getitem__(self, *args, **kwargs): # real signature unknown
        """ See PEP 585 """
        pass

    def __contains__(self, *args, **kwargs): # real signature unknown
        """ Return key in self. """
        pass

    def __delitem__(self, *args, **kwargs): # real signature unknown
        """ Delete self[key]. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __iadd__(self, *args, **kwargs): # real signature unknown
        """ Implement self+=value. """
        pass

    def __imul__(self, *args, **kwargs): # real signature unknown
        """ Implement self*=value. """
        pass

    def __init__(self, seq=()): # known special case of list.__init__
        """
        Built-in mutable sequence.
        
        If no argument is given, the constructor creates a new empty list.
        The argument must be an iterable if specified.
        # (copied from class doc)
        """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    def __mul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value. """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __reversed__(self, *args, **kwargs): # real signature unknown
        """ Return a reverse iterator over the list. """
        pass

    def __rmul__(self, *args, **kwargs): # real signature unknown
        """ Return value*self. """
        pass

    def __setitem__(self, *args, **kwargs): # real signature unknown
        """ Set self[key] to value. """
        pass

    def __sizeof__(self, *args, **kwargs): # real signature unknown
        """ Return the size of the list in memory, in bytes. """
        pass

    __hash__ = None

四、append

# 原列表
>>> name_list
['root', 'gm', 'hlr']

# 新增一个root值
>>> name_list.append("root")

# 新列表
>>> name_list
['root', 'gm', 'hlr', 'root']

五、count

# 统计root用户在列表中出现的次数
>>> name_list.count("root")
2

# 额外补充,如何删除列表中的root值(此时列表有2个值为root)
>>> name_list
['root', 'hlr', 'gm', 'root']
>>> for i in range(name_list.count("root")):
... name_list.remove("root")
... 

>>> name_list
['hlr', 'gm']

六、index

# 原列表
>>> name_list
['root', 'gm', 'hlr', 'root']

# 查看hlr用户在列表第一次出现的位置
>>> name_list.index("hlr")
2

七、insert

# 原列表
>>> name_list
['root', 'gm', 'hlr', 'root']

# 在列表的位置2处新增一个test用户
>>> name_list.insert(2, "test")

# 新列表
>>> name_list
['root', 'gm', 'test', 'hlr', 'root']

八、pop

# 原列表
>>> name_list
['root', 'gm', 'test', 'hlr', 'root']

# 删除列表位置为2的值
>>> name_list.pop(2)
'test'

# 新列表
>>> name_list
['root', 'gm', 'hlr', 'root']

# 删除列表最后一个值
>>> name_list.pop()
'root'

# 新列表
>>> name_list
['root', 'gm', 'hlr']

九、remove

# 原列表
>>> name_list
['root', 'gm', 'hlr']

# 新增一个gm用户
>>> name_list.append("gm")

# 新列表
>>> name_list
['root', 'gm', 'hlr', 'gm']

# 移除第一个gm用户
>>> name_list.remove("gm")

# 新列表
>>> name_list
['root', 'hlr', 'gm']

十、reverse

# 原列表
>>> name_list
['root', 'hlr', 'gm']

# 反向排列列表中元素
>>> name_list.reverse()

# 新列表
>>> name_list
['gm', 'hlr', 'root']

十一、sort

# 原列表
>>> name_list
['root', 'hlr', 'gm']

# 对列表进行排序
>>> name_list.sort()

# 新列表
>>> name_list
['gm', 'hlr', 'root']

更多列表操作

https://www.cnblogs.com/evescn/p/12395591.html

标签:__,name,Python,List,self,list,列表,root
From: https://www.cnblogs.com/evescn/p/17469512.html

相关文章

  • python3 del关键字
    1、介绍python中,del关键字可以用于销毁对象。一方面,可以用于实现业务,比如删除集合的元素。另一方面,可以节约内存资源,提升程序效率。 classStu:def__init__(self):self.name='abc'def__del__(self):print('del')stu_1=Stu()stu_2=S......
  • python基础day22 time和re模块
    time模块(跟时间打交道的模块)表示时间的三种方式1.时间戳:1970年1月1日到现在的秒数2.格式化的时间字符串:2023-01-0111:11:113.结构化时间:它是让计算机看的 导入time模块imporetimetime.time()#时间戳time.sleep(3)#睡眠3秒python中时间日期格式化符号%y......
  • 抗锯齿下采样(Anti-aliasing/down-sampling)-python-numpy 实现
    抗锯齿下采样(Anti-aliasing/down-sampling)-python-numpy实现这篇内容会涉及:卷积和抗锯齿下采样。代码请访问:https://github.com/LonglongaaaGo/ComputerVision问题描述如果直接对图片进行上采样,比如说用nearest线性插值,我们能够发现上采样的图片会有很多锯齿,如上篇从Nearest插值......
  • Python基础之时间模块、随机模块
    时间模块time模块'''time模块是函数内置的模块可以直接拿来用的'''importtime#时间的3种格式1、时间戳:从1970年到现在经过的秒数 作用:用于时间间隔的计算print(time.time()) #1686229427.28574542、按照某种时间格式显示的时间:2023-06-0821:03:47strftime()......
  • Python进阶:进程的状态及基本操作
    文章目录Python进阶篇-系列文章全篇一、进程以及状态二、[重点]进程-基本使用三、[重点]进程-名称、PID四、[重点]进程-参数传递、全局变量问题五、[重点]进程-守护主进程六、进程、线程对比七、[重点]消息队列-基本操作八、消息队列-常见判断九、[重点]Queue实现进程间通信十、[......
  • Python进阶:利用线程实现多任务
    文章目录Python进阶篇-系列文章全篇一、多任务的介绍二、[重点]线程-基本使用三、[重点]线程-线程名称、总数量四、[重点]线程-参数及顺序五、[重点]线程-守护线程六、并行和并发七、[重、难点]自定义线程类八、[重点]多线程-共享全局变量九、[难点]多线程-共享全局变量-问......
  • python tkinter 动态批量建立Widget时,combobox 或 entry传递参数问题
    terminal_combobox.bind('<<ComboboxSelected>>',lambdaevent,arg=key_dict:self.terminal_select(key_dict=arg))#注意,传递参数方法defterminal_select(self,key_dict,*args):var=self.dict_widget[key_d......
  • python爬虫概念
    Python爬虫是指使用Python编写程序来自动化地提取互联网上的信息(如文本、图像、视频、音频等)。它通常使用HTTP协议向Web服务器发送请求,并通过解析HTML响应来提取所需的信息。Python爬虫可以用于数据挖掘、信息收集、自动化测试等任务。常用的Python爬虫库包括BeautifulSoup、lxml......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1:task1_1实验源码:1fromturtleimport*234defmove(x,y):5penup()6goto(x,y)7pendown()8910defdraw(n,size=100):11foriinrange(n):12fd(size)13left(360/n)141516defmain():17......
  • python - execjs使用crypto-js
    最近在研究一个网站发现网站使用了des加密,觉得使用python调用js可读性比较高,所以使用了以下方法来实现该网站的内容解密1.安装PyExecJSpip3installPyExecJs2.安装node.jshttps://nodejs.org/en/download3.node安装jsdom,crypto-js可以到py文件目录在运行npm,方便调用np......