python列表,字符串,字典,集合常用方法

Posted 张子恒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python列表,字符串,字典,集合常用方法相关的知识,希望对你有一定的参考价值。

1.列表常用方法

#1. append用于在列表末尾追加新的对象
a = [1,2,3]
a.append(4)                          #the result : [1, 2, 3, 4]

#2. count方法统计某个元素在列表中出现的次数
a = [\'aa\',\'bb\',\'cc\',\'aa\',\'aa\']
print(a.count(\'aa\'))                 #the result : 3

#3. extend方法可以在列表的末尾一次性追加另一个序列中的多个值
a = [1,2,3]
b = [4,5,6]
a.extend(b)                          #the result :[1, 2, 3, 4, 5, 6]

#4. index函数用于从列表中找出某个值第一个匹配项的索引位置
a = [1,2,3,1]
print(a.index(1))                   #the result : 0

#5. insert方法用于将对象插入到列表中
a = [1,2,3]
a.insert(0,\'aa\')            #the result : [\'aa\', 1, 2, 3]

#6. pop方法会移除列表中的一个元素(默认是最后一个),并且返回该元素的值
a = [1,2,3]
a.pop()                             #the result : [1, 2]
a.pop(0)

#7. remove方法用于移除列表中某个值的第一个匹配项
a = [\'aa\',\'bb\',\'cc\',\'aa\']
a.remove(\'aa\')                      #the result : [\'bb\', \'cc\', \'aa\']

#8. reverse方法将列表中的元素反向存放
a = [\'a\',\'b\',\'c\']
a.reverse()                         #the result : [\'c\', \'b\', \'a\']

#9. sort方法用于在原位置对列表进行排序,意味着改变原来的列表,让其中的元素按一定顺序排列
a = [\'a\',\'b\',\'c\',1,2,3]
a.sort()                           #the result :[1, 2, 3, \'a\', \'b\', \'c\']

#10. enumrate
li = [11,22,33]
for k,v in enumerate(li, 1):
    print(k,v)

2.字符串常用方法

 

#1. find方法可以在一个较长的字符串中查找子串,他返回子串所在位置的最左端索引,如果没有找到则返回-1
a = \'abcdefghijk\'
print(a.find(\'abc\'))                                  #the result : 0
print(a.find(\'abc\',10,100))                           #the result : 11  指定查找的起始和结束查找位置

#2. join方法是非常重要的字符串方法,他是split方法的逆方法,用来连接序列中的元素,并且需要被连接的元素都必须是字符串。
a = [\'1\',\'2\',\'3\']
print(\'+\'.join(a))                                    #the result : 1+2+3

#3. split方法,是一个非常重要的字符串,它是join的逆方法,用来将字符串分割成序列
print(\'1+2+3+4\'.split(\'+\'))                          #the result : [\'1\', \'2\', \'3\', \'4\']

#4. strip 方法返回去除首位空格(不包括内部)的字符串
print("   test   test    ".strip())                  #the result :“test   test”

#5. replace方法返回某字符串所有匹配项均被替换之后得到字符串
print("This is a test".replace(\'is\',\'is_test\'))     #the result : This_test is_test a test

 

3.字典常用方法

#1. clear方法清除字典中所有的项,这是一个原地操作,所以无返回值(或则说返回None)
d = {\'name\':"tom"}
d.clear()
print(d)                                         #the result : {}

#2. fromkeys方法使用给定的键建立新的字典,每个键都对应一个默认的值None
print({}.fromkeys([\'name\',\'age\']))              #the result : {\'age\': None, \'name\': None}

#3. get方法是个更宽松的访问字典项的方法,如果试图访问字典中不存在的项时不会报错仅会      返回:None
d = {\'Tom\':8777,\'Jack\':8888,\'Fly\':6666}
print(d.get(\'Tom\'))                              #the result :     8777
print(d.get(\'not_exist\'))                       #the result :     None

#4. for循环字典的三种方法
d = {\'Tom\':8777,\'Jack\':8888,\'Fly\':6666}
for k,v in d.items():
    print(k,v)
for k in d.values():
    print(k)
for k in d.keys():
    print(k)

#5. pop方法用于获得对应与给定键的值,然后将这个”键-值”对从字典中移除
d = {\'Tom\':8777,\'Jack\':8888,\'Fly\':6666}
v = d.pop(\'Tom\')
print(v)                    #8777


#6. setdefault方法在某种程度上类似于get方法,能够获得与给定键相关联的值,除此之外,setdefault还能在字典中不含有给定键的情况下设定相应的键值
d = {\'Tom\':8777,\'Jack\':8888,\'Fly\':6666}
d.setdefault(\'Tom\')                           #the result : 8777
print(d.setdefault(\'Test\'))                  #the result : None
print(d)                                      #{\'Fly\': 6666, \'Jack\': 8888, \'Tom\': 8777, \'Test\': None}

#7. update方法可以利用一个字典项更新另一个字典,提供的字典中的项会被添加到旧的字典中,如有相同的键则会被覆盖
d = {\'Tom\':8777,\'Jack\':8888,\'Fly\':6666}
a = {\'Tom\':110,\'Test\':119}
d.update(a)
print(d)                                       #the result :{\'Fly\': 6666, \'Test\': 119, \'Jack\': 8888, \'Tom\': 110}

#8. 将两个列表组合成字典
keys = [\'a\', \'b\']
values = [1, 2]
print(dict(zip(keys,values)))                                      # {\'a\': 1, \'b\': 2}

4.集合常用方法

list_1 = [1,2,3,4,5,1,2]
#1、去重(去除list_1中重复元素1,2)
list_1 = set(list_1)                                    #去重: {1, 2, 3, 4, 5}
print(list_1)
list_2 = set([4,5,6,7,8])

#2、交集(在list_1和list_2中都有的元素4,5)
print(list_1.intersection(list_2))                      #交集: {4, 5}

#3、并集(在list_1和list_2中的元素全部打印出来,重复元素仅打印一次)
print(list_1.union(list_2))                             #并集: {1, 2, 3, 4, 5, 6, 7, 8}

#4、差集
print(list_1.difference(list_2))                        #差集:在list_1中有在list_2中没有:   {1, 2, 3}
print(list_2.difference(list_1))                        #差集:在list_1中有在list_2中没有:   {8, 6, 7}

以上是关于python列表,字符串,字典,集合常用方法的主要内容,如果未能解决你的问题,请参考以下文章

python字典(dict)+常用方法操作+列表元组集合字典的互相转换

Python 基础入门笔记———— 字符串列表元组集合字典

Python字符串列表元组字典集合

Python字符串列表元组字典集合

9. python 入门教程快速复习,序列,数值类型,字符串方法,列表集合字典方法,文件操作,解析式

Python第二周之字符串,列表,元组,集合,字典