Python基本类型的方法总结

 

目录

int类型

str类型

list类型

dict类型

tuple类型


int类型

  • int(self, x, base = 10) 

用途:用于将字符串转为整型数字(十进制)。注:在base默认为10的情况下,字符串只能是纯数字

返回值:十进制数。

其中,x表示需要转换的字符串;

           base表示x的进制,有效的进制是0和2-36(注:如果base存在,int()表示将base进制的x转换为十进制的整型数);

例:

>>> number = "a" 
>>> print(int(number, base = 16)) #number字符串为十六进制,因此base=16
>>> 10 #输出为10

>>> number = "0b0110"   #0b表示二进制
>>> print(int(number, base = 2))  #number字符串为二进制,因此base=2
>>> 6 #输出为6
  • bit_length(self)

用途:计算以二进制表示的整型数的bit长度。

返回值:整型数的二进制长度

例:

>>> bin(8) #bin(self)返回一个整型数的二进制
>>> 0b1000

>>> (8).bit_length()
>>> 4   #因此8的二进制是1000,有效长度是4

 


str类型

  • capitalize(self, *args, **kwargs) 

用途:将字符串的首字母大写。

返回值:首字母大写后的字符串。

例:

>>> string = "python language is very good"
>>> string.capitalize()
>>> Python language is very good #输出结果,首字母P被大写,其他依旧保持
  • upper(self, *args, **kwargs)

用途:将整个字符串全部大写。

返回值:被大写的字符串。

例:

>>> string = "python language is very good"
>>> string.upper()
>>> PYTHON LANGUAGE IS VERY GOOD #字符串每个字符都大写
  • titile(self, *args, **kwargs)

用途:将字符串中每个单词的首字母大写。

返回值:每个单词首字母大写的字符串。

例:

>>> string = "python language is very good"
>>> string.title()
>>> Python Language Is Very Good #字符串中每个单词首字母大写

注:capitalize、upper、title大写的方式不一样,注意区别。

#有大写操作方法就会有小写操作方法

  • lower(self, *args, **kwargs)

用途:将整个字字符串全部小写。(注:与方法upper()是反例)

返回值:被小写的字符串。

例:

>>> string = "PYTHON LANGUAGE IS VERY GOOD"
>>> string.lower()
>>> python language is very good #输出整个字符串的小写

#有upper、title、lower形式的字符串,那么就肯定有判断这几个形式字符串的判断方法

  • isupper(self, *args, **kwargs)

用途:判断字符串是否是全部是大写。

返回值:如果字符串全部是大写,则返回True,否则返回False。

例:

>>> string = "PYTHON LANGUAGE IS VERY GOOD"  #全部大写
>>> string.isupper()
>>> True  #输出结果

>>> string1 = "PYTHON LANGUAGE is VERY GOOD"  #非全部大写
>>> string1.isupper()
>>> False #输出结果
  • istitle(self, *args, **kwargs)

用途:判断字符串每个单词的首字母是否大写。

返回值:如果字符串每个单词的首字母是大写,则返回True,否则返回False。

例:

>>> string = "Pyhton Language Is Very Good"  #首字母全部大写
>>> string.istitle()
>>> True  #输出结果

>>> string1 = "Python language is Very Good"  #首字母并非全部大写
>>> string1.istitle()
>>> False  #输出结果
  • islower(self, *args, **kwargs)

用途:判断字符串是否是全部是小写。

返回值:如果字符串全部是小写,则返回True,否则返回False。

例:

>>> string = "python language is very good"  #字符串全部小写
>>> string.islower()
>>> True  #输出结果

>>> string1 = "Python language is very good"  #字符串非全部小写
>>> string1.islower()
>>> False  #输出结果
  • center(self, width, fillchar=None)

用途:将字符串居中。

返回值:长度为width的居中字符串。

其中,width(int型)表示整个字符串需要的宽度;

           fillchar(str型)表示这个width中除了字符串以外的填充位,该值只能表示一个字符,默认None表示填充位为空白;

例:

>>> "python".center(20)
>>>        python         #填充fillchar默认为空白填充

>>> "python".center(20, "*")
>>> *******python*******  #输出结果,python已经居中,而其他填充位使用"*"填充

>>> "python".center(20, "中")
>>> 中中中中中中中python中中中中中中中  #输出结果,python已经居中,而其他填充位使用“中”填充
  • count(self, sub, start=None, end=None)

用途:统计子字符串sub在字符串中的个数。

返回值:子字符串sub在字符串中的个数。

其中,sub表示字符串中的子字符串;

           start表示字符串中的起始位置;

           end表示字符串中的结束位置。

例:

>>> "python language is very good".count("o")  #没有限制子字符串"o"统计的范围
>>> 3  #输出结果

>>> "python language is very good".count("o", 8, 28)  #字符串总长度为28
>>> 2  #输出结果,因为是从字符串8的位置开始,因此python中的“o”并不会统计在里面

>>> "python python python Python".count("python")
>>> 3  #输出结果,因为最后一个python的首字母P是大写与查找的不匹配,因此只有3个"python"
  • endswith(self, suffix, start=None, end=None)

用途:判断字符串的后面是否以suffix结尾。

返回值:字符串的后面是以suffix结尾,则返回True,否则返回False。

其中,suffix表示字符串后缀,该值可以是字符串或字符;

           start表示判断字符串后缀的起始位置;

           end表示判断字符串后缀的结束位置。

例:

>>> "python language is very good".endswith("good")  #没有限制判断范围
>>> True  #输出结果

>>> "python language is very good".endswith("good", 0, 10)  #限制了判断范围
>>> False  #输出结果,因为字符串长度为28,而判断范围只有在0-10,因此没有"good"
  • startswith(self, prefix, start=None, end=None)

用途:判断字符串的前面是否以prefix开始。

返回值:字符串的后面是以prefix开始,则返回True,否则返回False。

其中,prefix表示字符串前缀,该值可以是字符串或字符;

           start表示判断字符串前缀的起始位置;

           end表示判断字符串前缀的结束位置。

例:

>>> "python language is very good".startswith("python")  #没有限制判断范围
>>> True  #输出结果

>>> "python language is very good".startswith("python", 5, 10)  #限制了判断范围
>>> False  #输出结果,因为字符串长度为28,而判断范围只有在5-10,因此没有"python"
  • find(self, sub, start=None, end=None)

用途:查找子字符串sub在字符串中的位置(注:该方法是从字符串的前面开始查找)。

返回值:子字符串sub在字符串中的位置索引值,如果没有该子字符串sub,则返回-1。

其中,sub表示字符串中的子字符串;

           start表示字符串中的起始位置;

           end表示字符串中的结束位置。

例:

>>> "python language is very good".find("is")  #没有限制查找范围
>>> 16  #输出结果,为"is"中'i'匹配的索引位置

>>> "python language is very good".find("is", 0, 10)  #限制查找范围
>>> -1  #输出结果,因为is是在索引16,而范围只是0-10,因此查找不到
  • rfind(self, sub, start=None, end=None)

用途:查找子字符串sub在字符串中的位置(注:该方法是从字符串的后面开始查找)。

返回值:子字符串sub在字符串中的位置索引值,如果没有该子字符串sub,则返回-1。

其中,sub表示字符串中的子字符串;

           start表示字符串中的起始位置;

           end表示字符串中的结束位置。

例:

>>> "python language is very good".rfind("is")  #没有限制查找范围
>>> 16  #输出结果,为"is"中'i'匹配的索引位置,查找结果与find()一样

>>> "python language is very good".rfind("is", 0, 10)  #限制查找范围
>>> -1  #输出结果,因为is是在索引16,而范围只是0-10,因此查找不到
  • index(self, sub, start=None, end=None)

用途:查找子字符串sub在字符串中的位置(注:该方法是从字符串的前面开始查找,与方法find()差不多,如果查找不到sub,会直接报错,而不是返回-1,因此建议使用find方法)。

返回值:子字符串sub在字符串中的位置索引值,如果没有该子字符串sub,则报错。

其中,sub表示字符串中的子字符串;

           start表示字符串中的起始位置;

           end表示字符串中的结束位置。

例:

>>> "python language is very good".index("is")  #没有限制查找范围
>>> 16  #输出结果,为"is"中'i'匹配的索引位置

>>> "python language is very good".index("is", 0, 10)  #限制查找范围
>>> ValueError: substring not found  #输出结果,因为is是在索引16,而范围只是0-10,因此查找不到
  • rindex(self, sub, start=None, end=None)

用途:查找子字符串sub在字符串中的位置(注:该方法是从字符串的后面开始查找,与方法find()差不多,如果查找不到sub,会直接报错,而不是返回-1,因此建议使用find方法)。

返回值:子字符串sub在字符串中的位置索引值,如果没有该子字符串sub,则报错。

其中,sub表示字符串中的子字符串;

           start表示字符串中的起始位置;

           end表示字符串中的结束位置。

例:

>>> "python language is very good".rindex("is")  #没有限制查找范围
>>> 16  #输出结果,为"is"中'i'匹配的索引位置

>>> "python language is very good".rindex("is", 0, 10)  #限制查找范围
>>> ValueError: substring not found  #输出结果,因为is是在索引16,而范围只是0-10,因此查找不到
  • format(self, *args, **kwargs)

用途:格式化。

返回值:使用args和kwargs中的替换返回S的格式化版本。

例:

#format有两种格式化形式
#第一种:采用键值对应的形式,键值可在format中任意放置
>>> string = "I am {name}, {age} years old {year}."  #采用{}对键值进行确认
>>> string.format(age = "ten", name = 'python', year = 'this year')  #键值位置任意
>>> I am python, ten years old this year  #输出结果

#第二种:采用索引对应的形式,值的位置是确定的
>>> string = "I am {0}, {1} years old {2}."
>>> string.format("python", "ten", "this year")  #索引的位置与值是确定的,如同数组索引与值对应一样,不可任意
>>> I am python, ten years old this year  #输出结果

  • format_map(self, mapping)

用途:格式化(注:与format的区别在于,该方法是采用字典的形式进行格式化)。

返回值:使用mapping替换返回S的格式化版本。

例:

>>> string = "I am {name}, {age} years old {year}."  #采用{}对键值进行确认
>>> string.format({"age": "ten", "name": 'python', "year": 'this year'})  #键值位置任意
>>> I am python, ten years old this year  #输出结果
  • isalnum(self, *args, **kwargs)

用途:判断字符串是字母或者数字(注:字符串可以是纯字母或数字或字母和数字的组成,不包括其他特殊符号)。

返回值:如果字符串是字母或者数字或者两者的组合,则返回True,否则返回False。

例:

>>> "Python3".isalnum()
>>> True  #输出结果

>>> "python".isalnum()
>>> True  #输出结果

>>> "666".isalnum()
>>> True  #输出结果

>>> "Python3_".isalnum()
>>> False  #输出结果
  • isalpha(self, *args, **kwargs)

用途:判断字符串是否是字母(注:字符串只能是纯字母,不包括数字或其他特殊符号)。

返回值:如果字符串是字母,则返回True,否则返回False。

例:

>>> "Python3".isalpha()
>>> False  #输出结果

>>> "python".isalpha()
>>> True  #输出结果

>>> "666".isalpha()
>>> False  #输出结果

>>> "Python3_".isalpha()
>>> False  #输出结果
  • isdecimal(self, *args, **kwargs)

用途:判断字符串是否是十进制数字(注:该方法是对十进制数字进行判断)

返回值:如果字符串是十进制数字,则返回True,否则返回False。

例:

>>> "123".isdecimal()
>>> True  #输出结果

>>> "②".isdecimal()
>>> False  #输出结果
  • isdigit(self, *args, **kwargs)

用途:判断字符串是否是数字(注:该方法除了十进制数字还可以对②这类数字进行判断)

返回值:如果字符串是数字,则返回True,否则返回False。

例:

>>> "123".isdigit()
>>> True  #输出结果

>>> "②".isdigit()
>>> True  #输出结果

>>> "贰".isdigit()
>>> False  #输出结果
  • isnumeric(self, *args, **kwargs)

用途:判断字符串是否是数字(注:该方法比isdigit()方法更高级,一切数字都可以)

返回值:如果字符串是数字,则返回True,否则返回False。

例:

>>> "123".isnumeric()
>>> True  #输出结果

>>> "②".isnumeric()
>>> True  #输出结果

>>> "贰".isnumeric()
>>> True  #输出结果

>>> "二".isnumeric()
>>> True  #输出结果

>>> "Ⅱ".isnumeric()
>>> True  #输出结果

>>> "123b".isnumeric()
>>> False  #输出结果

注:isdecimal()、isdigit()、isnumeric()这三个判断数字的方法需要注意,如果只要判断十进制数字,建议使用方法isdeciaml()

  • isprintable(self, *args, **kwargs)

用途:判断字符串是否可打印(注:通常都可以打印,但字符串包含了不可显示的字符就会返回False,例如:\n, \t)

返回值:如果字符串可打印,则返回True,否则返回False。

例:

>>> "python".isprintable()
>>> True  #输出结果

>>> "python\tpython".isprintable()
>>> False  #输出结果
  • isspace(self, *args, **kwargs)

用途:判断字符串是否是空格。

返回值:如果字符串是空格,则返回True,否则返回False。

例:

>>> "python".isspace()
>>> False #输出结果

>>> " ".isspace()
>>> True  #输出结果
  • isidentifier(self, *args, **kwargs)

用途:判断字符串是否是标识符。(注:标识符可由数字,_,字母所组合,但不能是纯数字,空格,\n,\n

返回值:如果字符串是标识符,则返回True,否则返回False。

例:

>>> "python".isidentifier()
>>> True #输出结果

>>> " ".isidentifier()
>>> False  #输出结果

>>> "123".isidentifier()
>>> False  #输出结果

>>> "\n".isidentifier()
>>> False  #输出结果

>>> "\t".isidentifier()
>>> False  #输出结果
  • expandtabs(self, tabsize)

用途:用于控制字符串中制表符\t的长度。

返回值:使用空格替换所有制表符的副本。

           其中,tabsize表示制表符的长度

例:

>>> "python\t3".expandtabs(20)
>>> python              3   #输出结果
“””由于python和3中间有一个制表符,而制表符大小为20,因此python和3之间有14个空格,因为20的会碰到制表符的前面开始算起,也就是从p开始算,python共有6个字符,那么到制表符时已经有6个字符的大小了,然后后面补14个空格,共20“””
  • join(self, iterable)(⭐⭐⭐⭐⭐)

用途:用于连接任意数量的字符串。

返回值:将调用这个方法的字符串插入给定的字符串iterable之间的结果放回。

           其中,iterable表示被给定进行迭代操作的字符串

例:

>>> "_".join("python")
>>> p_y_t_h_o_n  #输出结果
#其中“python”是被进行迭代的字符串,而“_”是要插入迭代字符串中每个字符之间的字符串

>>> "我".join("python")
>>> p我y我t我h我o我n  #输出结果
  • ljust(self, width, fillchar=None)

用途:将字符串向左对齐。

返回值:长度为width的左对齐字符串。

其中,width(int型)表示整个字符串需要的宽度;

           fillchar(str型)表示这个width中除了字符串以外的填充位,该值只能表示一个字符,默认None表示填充位为空白;

例:

>>> "python".ljust(20, '*')
>>> python**************  #输出结果
  • rjust(self, width, fillchar=None)

用途:将字符串向右对齐。

返回值:长度为width的右对齐字符串。

其中,width(int型)表示整个字符串需要的宽度;

           fillchar(str型)表示这个width中除了字符串以外的填充位,该值只能表示一个字符,默认None表示填充位为空白;

例:

>>> "python".rjust(20, '*')
>>> **************python  #输出结果

>>> "python".rjust(20)
>>>               python  #输出结果,默认空白填充
  • strip(self, chars)(⭐⭐⭐⭐⭐)

用途:删掉左右两边的空白。

返回值:删掉两边指定字符的字符串。

其中,chars表示指定左右两边删掉的字符(默认为空白);

例:

>>> "  python".strip()
>>> python  #输出结果,该方法默认会自动匹配左右两边的空白,任意一边都空白都将被删除

>>> "  python  ".strip()
>>> python  #输出结果

>>> "abpythonc".strip("bac")
>>> python  #输出结果
#该方法会自动从左右两边去匹配指定的chars,chars就如一个子序列,然后调用strip方法的字符串,会从左右#两边一个字符一个字符去与chars进行匹配,匹配成功则删除,直到碰到chars不存在的字符则停止。例如:上述#chars=bac,字符串为"abpythonc",第一个字符为a,在chars中遍历一次,是否存在a字符,有则删除,然后b
#字符,当p字符时,chars中不存在p字符,则停止了;右边也是如此
  • lstrip(self, chars)

用途:删掉左边的空白。

返回值:删掉左边指定字符的字符串。

其中,chars表示指定左边删掉的字符(默认为空白);

例:

>>> "  python".lstrip()
>>> python  #输出结果,该方法默认会自动匹配左边的空白

>>> "  python  ".lstrip()
>>> python    #输出结果,右边还存在空白

>>> "abpythonc".lstrip("bac")
>>> pythonc  #输出结果,与strip类似,但lstrip方法只处理字符串左边
  • rstrip(self, chars)

用途:删掉右边的空白。

返回值:删掉右边指定字符的字符串。

其中,chars表示指定右边删掉的字符(默认为空白);

例:

>>> "  python".lstrip()
>>>   python  #输出结果,该方法默认会自动匹配右边的空白

>>> "  python  ".lstrip()
>>>   python  #输出结果,左边还存在空白

>>> "abpythonc".lstrip("bac")
>>> abpython  #输出结果,与strip类似,但rstrip方法只处理字符串右边
  • split(self, sep, maxsplit)(⭐⭐⭐⭐⭐)

用途:将字符串进行分割。

返回值:返回一个以sep进行分割后的列表。

其中,sep表示用于分割的分隔符;

           maxsplit表示最大被分割的数,默认-1表示全部分割

例:

>>> "python_python_python".split("_")
>>> ['python', 'python', 'python']  #输出结果,分割符"_"不会被包含在列表当中

>>> "python_python_python".split("_", 1)
>>> ['python', 'python_python']  #输出结果,方法split中参数maxsplit为1,因此只被分割一次

>>> "python_python_python".split("_", 2)
>>> ['python', 'python', 'python']  #输出结果,方法split中参数maxsplit为2,因此只被分割两次
  • rsplit(self, sep, maxsplit)

用途:将字符串从右边开始进行分割。

返回值:返回一个以sep进行分割后的列表。

其中,sep表示用于分割的分隔符;

           maxsplit表示最大被分割的数,默认-1表示全部分割

例:

>>> "python_python_python".rsplit("_")
>>> ['python', 'python', 'python']  #输出结果,分割符"_"不会被包含在列表当中

>>> "python_python_python".rsplit("_", 1)
>>> ['python_python', 'python']  #输出结果,方法split中参数maxsplit为1,因此只被分割一次

>>> "python_python_python".rsplit("_", 2)
>>> ['python', 'python', 'python']  #输出结果,方法split中参数maxsplit为2,因此只被分割两次
  • partition(self, sep)

用途:将字符串从进行分割。(注:与split不同,该方法返回元组,并且由三个部分组成,sep、sep左右两边的字符串)

返回值:返回一个元组,该元组由sep进行分割的三个部分。

其中,sep表示用于分割的分隔符

例:

>>> "python_python_python".partition("_")
>>> ('python', '_', 'python_python')  #输出结果,分割符"_"会被包含在元组当中,并且从左边开始,找到第一个sep就进行分割
  • rpartition(self, sep)

用途:将字符串从进行分割。(注:与split不同,该方法返回元组,并且由三个部分组成,sep、sep左右两边的字符串)

返回值:返回一个元组,该元组由sep进行分割的三个部分。

其中,sep表示用于分割的分隔符

例:

>>> "python_python_python".partition("_")
>>> ('python_python', '_', 'python')  #输出结果,分割符"_"会被包含在元组当中,并且从右边开始,找到第一个sep就进行分割
  • replace(self, old, new, count)(⭐⭐⭐⭐⭐)

用途:将字符串从进行替换。

返回值:返回一个被替换后的字符串。

其中,old表示需要被替换的字符串

           new表示替换字符串的新字符串

           count表示需要被替换字符串的个数,默认-1,全部替换

例:

>>> "python_python_python".replace("_", "*")
>>> python*python*python  #输出结果,字符串"_"被替换成了"*"

>>> "python_python_python".replace("_", "*", 1)
>>> python*python_python  #输出结果,字符串"_"被替换成了"*"

>>> "python_python_python".replace("_", "*", 2)
>>> python*python*python  #输出结果,字符串"_"被替换成了"*"
  • swapcase(self, *args, **kwargs)

用途:将字符串大写进行小写,小写进行大写。

返回值:返回一个大小写反转后的字符串。

例:

>>> "PyThOn".swapcase()
>>> pYtHoN  #输出结果

 

list类型

  • append(self, object)

用途:在list的末尾添加一个object。

返回值:返回一个添加object的list。

       其中,object表示被添加目标(可以是任意类型)。

例:

>>> test = ['python']
>>> test.append(3)
>>> test
['python', 3]  #输出结果

>>> test.append([1, 2])  #添加一个列表
>>> test
['python', 3, [1, 2]]  #输出结果

>>> test.append((5, 4,))  #添加一个元组
>>> test
['python', 3, [1, 2], (4, 5)]  #输出结果

>>> test.append(2.7)  #添加一个小数
>>> test
['python', 3, [1, 2], (4, 5), 2.7]
  • clear(self)

用途:将list清空。

返回值:返回一个空的list。

例:

>>> test = ["python"]
>>> test
[]  #输出结果,一个空的列表
  • copy(self)

用途:复制一份列表。

返回值:返回列表的一个副本(修改副本不会改变原有的列表)。

例:

>>> test = ["python"]
>>> testCopy = test.copy()
>>> testCopy
["python"]  #输出结果

>>> testCopy.append(3)
>>> testCopy
["python", 3]  #输出结果

>>> test
["python"]  #输出结果,改变其副本,并不会改变原有列表
  • count(self, object)

用途:在list中统计object的个数。

返回值:返回object的个数。

       其中,object表示被统计的元素(可以是任意类型)。

例:

>>> test = ["python"]
>>> test.count("python")
1  #输出结果

>>> test.append("python")
>>> test.count("python")
2  #输出结果
  • extend(self, iterable)

用途:在list的末尾添加元素。(注:区分append和extend的区别)

返回值:返回在列表中添加元素后的list。

       其中,iterable表示能够迭代的元素(数字不能迭代)。

例:

>>> test = ["python"]
>>> test.extend([1, 2, 3])
>>> test
['python', 1, 2, 3]  #输出结果

>>> test = ["python"]
>>> test.append([1, 2, 3])  #区分extend和append
>>> test
['python', [1, 2, 3]]  #输出结果

>>> test = ["python"]
>>> test.extend("extend")
>>> test
['python', 'e', 'x', 't', 'e', 'n', 'd']  #输出结果,如果是字符串,则会一个字符一个字符的迭代添加

>>> test.extend((1,2,3,))  #添加一个元组
>>> test
['python', 'e', 'x', 't', 'e', 'n', 'd', 1, 2, 3]  #输出结果

>>> test.extend(666)  #数字不能迭代
Traceback (most recent call last):
  File "<pyshell#40>", line 1, in <module>
    test.extend(666)
TypeError: 'int' object is not iterable
  • index(self, object, start, stop)

用途:在list中查找object的索引。

返回值:返回object在list中的的位置索引。

       其中,object表示需要查找的元素;

                 start表示list中查找的起始位置;

                 stop表示list中查找的末尾位置。

例:

>>> test = ['python', 'e', 'x', 't', 'e', 'n', 'd', 1, 2, 3]
>>> test.index("e")
1  #输出结果,从左边至又开始查,如果有相同的元素,只会返回第一个元素的索引

>>> test.index("e", 2, 9)  #指定起始位置和终止位置
4
  • insert(self, index, object)

用途:在list中的index位置插入object元素。

返回值:返回插入元素object后的list。

       其中,index表示插入元素的位置;

                 object表示插入的元素。

例:

>>> test = ["python", "insert"]
>>> test.insert(1, [2, 3])  #在索引1处插入列表[2, 3]
>>> test
['python', [2, 3], 'insert']  #输出结果

>>> test.insert(2, (666, 888))  #在索引2处插入元组(666, 888)
>>> test
['python', [2, 3], (666, 888), 'insert']  #输出结果
  • pop(self, index)

用途:在list中的移除index位置元素。

返回值:返回移除指定index处的元素。

       其中,index表示移除元素的位置(默认为list末尾的元素索引)。

例:

>>> test = ['python', [2, 3], (666, 888), 'insert']
>>> test.pop()
'insert'  #输出结果

>>> test.pop(1)
[2, 3]  #输出结果

>>> test
['python', (666, 888)]  #输出结果
  • remove(self, object)

用途:在list中的移除object元素。

返回值:无。

       其中,object表示要被移除的元素。

       例:

>>> test = ['python', (666, 888)]
>>> test.remove('python')
>>> test
[(666, 888)]  #输出结果
  • reverse(self)

用途:将list中所有的元素的进行反转。

返回值:无。

       例:

>>> test = ['python', 'e', 'x', 't', 'e', 'n', 'd', 1, 2, 3]
>>> test.reverse()
>>> test
[3, 2, 1, 'd', 'n', 'e', 't', 'x', 'e', 'python']  #输出结果
  • sort(self, key, reverse)

用途:将list中所有的元素进行排序。

返回值:无。

       其中,reverse如果为True则以逆顺进行排序,默认为False,则为正顺排序

       例:

>>> test = [3, 1, 999, 3, 5, 234]
>>> test.sort()
>>> test
[1, 3, 3, 5, 234, 999]  #输出结果

>>> test.sort(reverse = True)  #逆顺排序
>>> test
[999, 234, 5, 3, 3, 1]  #输出结果

>>> test = [3, 2, 1, 'd', 'n', 'e', 't', 'x', 'e', 'python']
>>> test.sort()  #字符串和数字不能混合排序
Traceback (most recent call last):
  File "<pyshell#61>", line 1, in <module>
    test.sort()
TypeError: '<' not supported between instances of 'str' and 'int'

>>> test = ["python", 'insert']
>>> test.sort()  #对字符串进行排序
>>> test
['insert', 'python']  #输出结果

 

dict类型

 

tuple类型

 

未完待续......

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

5G加油站

你的鼓励是创造的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值