字符串、列表的一些操作汇总

下面表格给出本文主要总结的操作,后面详述每一种操作。

1. s1.join(s2):不改变原来s2

s1:必须为str;

s2:可以为str、list、tuple、dict (对key值做操作)、collection;

s1.join(s2):一定为str;

通过下面例子理解。

>>> s1='----'
>>> str1='python'
>>> list1=['hello','how','are','you']
>>> collection1={'123','234','456'}
>>> tuples1=('apple','bear','banana')
>>> dic1={'name':'wang','num':'234'}
>>> s1.join(str1)                       #对字符串中每个字符间进行'----'连接
'p----y----t----h----o----n'
>>> s1.join(list1)                      #对列表中每个元素间进行'----'连接
'hello----how----are----you'
>>> s1.join(collection1)                #对集合中每个元素间进行'----'连接(无序)
'456----123----234'
>>> s1.join(collection1)
'456----123----234'
>>> s1.join(tuples1)                    #对元祖中每个元素间进行'----'连接
'apple----bear----banana'
>>> s1.join(dic1)                       #对字典中每个key进行'----'连接
'name----num'
>>>
>>> str1                                #join操作未改变原来str1
'python'
>>> list1                               #join操作未改变原来list1
['hello', 'how', 'are', 'you']
>>> collection1                         #join操作未改变原来collection1 (无序)
{'456', '123', '234'}
>>> tuples1                             #join操作未改变原来tuples1
('apple', 'bear', 'banana')
>>> dic1                                #join操作未改变原来dic1
{'name': 'wang', 'num': '234'}
>>>

你会发现下面输出的collection1和上面输入的collection1中元素顺序不同,这是因为集合中元素是无序的,即元素位置可变,但集合本身不变,结合下面例子理解下。

>>> collection1={'123','234','456'}
>>> collection2={'234','123','456'}
>>> if collection1==collection2:
	print('yes')

	
yes
>>>

2. len(s2) :不改变原来s2

s2:可以为str、list、tuple、dict 、collection;

继续上面例子并结合下面例子理解。

>>> len(str1)
6
>>> len(list1)
4
>>> len(collection1)
3
>>> len(tuples1)
3
>>> len(dic1)
2
>>>

3. s1 in s2:不改变原来s2

s1:s1是s2的元素,所以可以是str、list、tuple、collection、dict;

s2:可以为str、list、tuple、dict (对key进行操作) 、collection;

继续上面例子并结合下面例子理解。

>>> 'y' in str1                 #s1为str1中的每个字符
True
>>> 'hello' in list1            #s1为list1中的每个元素
True
>>> '456' in collection1        #s1为collection1中的每个元素
True
>>> 'apple' in tuples1          #s1为tuples1中的每个元素
True
>>> 'name' in dic1              #s1为dic1中的每个key值
True
>>> 'wang' in dic1
False
>>> 'num' in dic1
True
>>> '234' in dic1
False
>>> 

4. s2*n :不改变原来s2

s2:可以为 str、list、tuple;

n:为整数

>>> str1 *2          #字符串可乘整数
'pythonpython'
>>> list1 * 2        #列表可乘整数
['hello', 'how', 'are', 'you', 'hello', 'how', 'are', 'you']
>>> collection1 * 2
Traceback (most recent call last):
  File "<pyshell#166>", line 1, in <module>
    collection1 * 2
TypeError: unsupported operand type(s) for *: 'set' and 'int'
>>> tuples1 * 2      #元祖可乘整数
('apple', 'bear', 'banana', 'apple', 'bear', 'banana')
>>> dic1 * 2
Traceback (most recent call last):
  File "<pyshell#168>", line 1, in <module>
    dic1 * 2
TypeError: unsupported operand type(s) for *: 'dict' and 'int'

5. s2+str2/list2:不改变原来s2

s2:可以为 str、list;

>>> str1 + 'fine'                 #字符串可加字符串
'pythonfine'
>>> list1 + ['f','i','n','e']     #列表可加列表
['hello', 'how', 'are', 'you', 'f', 'i', 'n', 'e']
>>> collection1 +{'fine'}
Traceback (most recent call last):
  File "<pyshell#171>", line 1, in <module>
    collection1 +{'fine'}
TypeError: unsupported operand type(s) for +: 'set' and 'set'
>>> tuples1 + 'fine'
Traceback (most recent call last):
  File "<pyshell#172>", line 1, in <module>
    tuples1 + 'fine'
TypeError: can only concatenate tuple (not "str") to tuple
>>> dic1 + 'fine'
Traceback (most recent call last):
  File "<pyshell#173>", line 1, in <module>
    dic1 + 'fine'
TypeError: unsupported operand type(s) for +: 'dict' and 'str'
>>>

6. max(s2)/min(s2):不改变原来s2

s2:可以是 str、list、collection、tuple、dict (对key值进行操作);

继续上面的例子理解。

>>> max(str1)         #对字符串中的字符进行比较
'y'
>>> min(str1)
'h'
>>> max(list1)        #对列表中元素进行比较
'you'
>>> min(list1)
'are'
>>> max(collection1)  #对集合中的元素进行比较
'456'
>>> min(collection1)
'123'
>>> max(tuples1)      #对元祖中的元素进行比较
'bear'
>>> min(tuples1)
'apple'
>>> max(dic1)         #对字典中的key值进行比较
'num'
>>> min(dic1)
'name'
>>>

7. list(s2):不改变原来s2

s2:可以是 str、list、collection、tuple、dict (对key值进行操作);

>>> list(str1)                      #对字符串中每个字符操作
['p', 'y', 't', 'h', 'o', 'n']
>>> list(tuples1)                   #对元祖中每个元素操作
['apple', 'bear', 'banana']
>>> list(collection1)               #对集合中每个元素操作
['456', '123', '234']
>>> list(dic1)                      #对字典中的key值操作
['name', 'num']
>>> list(list1)                     #对列表中每个元素操作
['hello', 'how', 'are', 'you']

8. s2[n1:n2]:不改变原来s2

s2:可以是 str、list、tuple;

n1,n2都是下标,包含n1,不包含n2.

>>> str1[2:4]                         #字符串可以分片
'th'
>>> list1[2:4]                         #列表可以分片
['are', 'you']
>>> collection1[0:2]
Traceback (most recent call last):
  File "<pyshell#193>", line 1, in <module>
    collection1[0:2]
TypeError: 'set' object is not subscriptable
>>> tuples1[0:2]                    #元祖可以分片
('apple', 'bear')
>>> dic1[0:1]
Traceback (most recent call last):
  File "<pyshell#195>", line 1, in <module>
    dic1[0:1]
TypeError: unhashable type: 'slice'

9. del s2[n]:改变原来的s2

s2:只能是 list。

>>> del str1[0]
Traceback (most recent call last):
  File "<pyshell#198>", line 1, in <module>
    del str1[0]
TypeError: 'str' object doesn't support item deletion
>>> del list1[0]                   #只有list能够进行del list1[0]操作
>>> list1
['how', 'are', 'you']
>>> del collection1[0]
Traceback (most recent call last):
  File "<pyshell#201>", line 1, in <module>
    del collection1[0]
TypeError: 'set' object doesn't support item deletion
>>> del tuples1[0]
Traceback (most recent call last):
  File "<pyshell#202>", line 1, in <module>
    del tuples1[0]
TypeError: 'tuple' object doesn't support item deletion
>>> del dic1[0]
Traceback (most recent call last):
  File "<pyshell#203>", line 1, in <module>
    del dic1[0]
KeyError: 0
>>> 

10. s1.append(s2):改变原来的s1

s1:只能是 list;

s2:可以是 str、list、collection、tuple、dict ( 整个字典操作);

>>> list2=['I','m','fine','thanks']
>>> list2.append(str1)        #将str1整个作为list2的最后一个元素
>>> list2
['I', 'm', 'fine', 'thanks', 'python']
>>> list2.append(list1)       #将list1整个作为list2的最后一个元素
>>> list2
['I', 'm', 'fine', 'thanks', 'python', ['hello', 'how', 'are', 'you']]
>>> list2.append(collection1) #将collection1整体作为list2的最后一个元素
>>> list2
['I', 'm', 'fine', 'thanks', 'python', ['hello', 'how', 'are', 'you'], {'456', '123', '234'}]
>>> list2.append(tuples1)     #将tuples1整体作为list2的最后一个元素
>>> list2
['I', 'm', 'fine', 'thanks', 'python', ['hello', 'how', 'are', 'you'], {'456', '123', '234'}, ('apple', 'bear', 'banana')]
>>> list2.append(dic1)        #将dic1整体作为list2的最后一个元素
>>> list2
['I', 'm', 'fine', 'thanks', 'python', ['hello', 'how', 'are', 'you'], {'456', '123', '234'}, ('apple', 'bear', 'banana'), {'name': 'wang', 'num': '234'}]

11. s1.extend(s2):改变原来的s1

s1:只能是 list;

s2:可以是 str、list、collection、tuple、dict;

>>> list2=['I','m','fine','thanks']
>>> list2.extend(list1)        #将list1中每个元素添加到list2中
>>> list2
['I', 'm', 'fine', 'thanks', 'hello', 'how', 'are', 'you']
>>> list2.extend(str1)         #先list(str1)再将每个元素添加到list2中
>>> list2
['I', 'm', 'fine', 'thanks', 'hello', 'how', 'are', 'you', 'p', 'y', 't', 'h', 'o', 'n']
>>> list2.extend(collection1)  #先list(collection1)再将每个元素添加到list2中
>>> list2
['I', 'm', 'fine', 'thanks', 'hello', 'how', 'are', 'you', 'p', 'y', 't', 'h', 'o', 'n', '456', '123', '234']
>>> list2.extend(tuples1)      #先list(tuples1)再将每个元素添加到list2中
>>> list2
['I', 'm', 'fine', 'thanks', 'hello', 'how', 'are', 'you', 'p', 'y', 't', 'h', 'o', 'n', '456', '123', '234', 'apple', 'bear', 'banana']
>>> list2.extend(dic1)         #先list(dic1)再将每个元素添加到list2中(每个key值)
>>> list2
['I', 'm', 'fine', 'thanks', 'hello', 'how', 'are', 'you', 'p', 'y', 't', 'h', 'o', 'n', '456', '123', '234', 'apple', 'bear', 'banana', 'name', 'num']
>>>

12. s1.index(s2):不改变原来s1

s1:可以是 str、list、tuple;

s2:s2是s1中的元素,当s1是列表时,s2所以可以是 str、list、collection、tuple、dict;

>>> str1.index('p')         #字符串可以进行index操作
0
>>> list1.index('are')      #列表可以进行index操作
2
>>> collection1.index('123')
Traceback (most recent call last):
  File "<pyshell#233>", line 1, in <module>
    collection1.index('123')
AttributeError: 'set' object has no attribute 'index'
>>> tuples1.index('bear')   #元祖可以进行index操作
1
>>> dic1.index('num')
Traceback (most recent call last):
  File "<pyshell#235>", line 1, in <module>
    dic1.index('num')
AttributeError: 'dict' object has no attribute 'index'
>>> list2=['fine']
>>> list2.append(str1)
>>> list2.append(list1)
>>> list2.append(collection1)
>>> list2.append(tuples1)
>>> list2.append(dic1)
>>> list2
['fine', 'python', ['hello', 'how', 'are', 'you'], {'456', '123', '234'}, ('apple', 'bear', 'banana'), {'name': 'wang', 'num': '234'}]
>>> list2.index('python')
1
>>> list2.index(['hello', 'how', 'are', 'you'])
2
>>> list2.index({'456', '123', '234'})
3
>>> list2.index(('apple', 'bear', 'banana'))
4
>>> list2.index({'name': 'wang', 'num': '234'})
5
>>> 

13. s1.count(s2):不改变原来s1

s1:可以是 str、list、tuple;

s2:s2是s1的元素,当s1是列表时,s2可以是 str、list、collection、tuple、dict;

>>> str1.count('p')            #字符串可以进行count操作
1
>>> list1.count('are')         #列表可以进行count操作
1
>>> tuples1.count('apple')     #元祖可以进行count操作
1
>>> collection1.count('123')
Traceback (most recent call last):
  File "<pyshell#251>", line 1, in <module>
    collection1.count('123')
AttributeError: 'set' object has no attribute 'count'
>>> dic1.count('num')
Traceback (most recent call last):
  File "<pyshell#252>", line 1, in <module>
    dic1.count('num')
AttributeError: 'dict' object has no attribute 'count'
>>> list2
['fine', 'python', ['hello', 'how', 'are', 'you'], {'456', '123', '234'}, ('apple', 'bear', 'banana'), {'name': 'wang', 'num': '234'}]
>>> list2.count('python')
1
>>> list2.count(['hello', 'how', 'are', 'you'])
1
>>> list2.count({'456', '123', '234'})
1
>>> list2.count(('apple', 'bear', 'banana'))
1
>>> list2.count({'name': 'wang', 'num': '234'})
1
>>> 

14. s1.insert(index,s2):改变原来s1

s1:只能是 list;

s2:可以是 str、list、collection、tuple、dict;

>>> list2=['fine']
>>> list2.insert(0,str1)        #插入字符串作为元素
>>> list2
['python', 'fine']
>>> list2.insert(0,list1)       #插入列表作为元素
>>> list2
[['hello', 'how', 'are', 'you'], 'python', 'fine']
>>> list2.insert(1,collection1) #插入集合作为元素
>>> list2
[['hello', 'how', 'are', 'you'], {'456', '123', '234'}, 'python', 'fine']
>>> list2.insert(1,tuples1)     #插入元祖作为元素
>>> list2
[['hello', 'how', 'are', 'you'], ('apple', 'bear', 'banana'), {'456', '123', '234'}, 'python', 'fine']
>>> list2.insert(0,dic1)        #插入字典作为元素
>>> list2
[{'name': 'wang', 'num': '234'}, ['hello', 'how', 'are', 'you'], ('apple', 'bear', 'banana'), {'456', '123', '234'}, 'python', 'fine']
>>> 

15. s1.pop(index):改变原来的s1

s1:只能是 list;

index:为空,默认删除最后一个元素;不为空,删除index的元素;

>>> list2
[{'name': 'wang', 'num': '234'}, ['hello', 'how', 'are', 'you'], ('apple', 'bear', 'banana'), {'456', '123', '234'}, 'python', 'fine']
>>> list2.pop()      #index为空,删除最后一个元素
'fine'
>>> list2
[{'name': 'wang', 'num': '234'}, ['hello', 'how', 'are', 'you'], ('apple', 'bear', 'banana'), {'456', '123', '234'}, 'python']
>>> list2.pop(0)     #删除index=0的元素
{'name': 'wang', 'num': '234'}
>>> list2
[['hello', 'how', 'are', 'you'], ('apple', 'bear', 'banana'), {'456', '123', '234'}, 'python']
>>> list2.pop(1)     #删除index=1的元素
('apple', 'bear', 'banana')
>>> list2
[['hello', 'how', 'are', 'you'], {'456', '123', '234'}, 'python']
>>> 

16. s1.remove(s2):改变原来的s1

s1:只能是 list;

s2:可以是 str、list、collection、tuple、dict;

>>> list2
['python', {'name': 'wang', 'num': '234'}, ['hello', 'how', 'are', 'you'], ('apple', 'bear', 'banana'), {'123', '456', '234'}, 'python', 'fine']
>>> list2.remove('python')      #只删除第一个'python'元素
>>> list2
[{'name': 'wang', 'num': '234'}, ['hello', 'how', 'are', 'you'], ('apple', 'bear', 'banana'), {'123', '456', '234'}, 'python', 'fine']
>>> list2.remove(str1)          #删除str元素
>>> list2
[{'name': 'wang', 'num': '234'}, ['hello', 'how', 'are', 'you'], ('apple', 'bear', 'banana'), {'123', '456', '234'}, 'fine']
>>> list2.remove(list1)         #删除list元素
>>> list2
[{'name': 'wang', 'num': '234'}, ('apple', 'bear', 'banana'), {'123', '456', '234'}, 'fine']
>>> list2.remove(collection1)   #删除集合元素
>>> list2
[{'name': 'wang', 'num': '234'}, ('apple', 'bear', 'banana'), 'fine']
>>> list2.remove(tuples1)       #删除元组元素
>>> list2
[{'name': 'wang', 'num': '234'}, 'fine']
>>> list2.remove(dic1)          #删除字典元素
>>> list2
['fine']
>>> 

17. s1.clear()/del s1[:]

s1:只能是 list;

>>> list2
['fine']
>>> list2.clear()
>>> list2
[]
>>> 

18. s1.copy()与'='赋值的区别

s1:只能是 list;

可以这样理解:

copy()操作只是将列表的内容复制了一份,被复制列表的变化不会影像到新的列表。

'='操作类似C中将该列表的地址指针复制,被复制列表的变化相当于地址内的内容变化,从而影像新列表的内容。

>>> list1
['hello', 'how', 'are', 'you']
>>> list2=list1
>>> list2
['hello', 'how', 'are', 'you']
>>> list3=list1.copy()
>>> list3
['hello', 'how', 'are', 'you']
>>> list1.insert(0,'fine')
>>> list1
['fine', 'hello', 'how', 'are', 'you']
>>> list2                   #'='随着list1插入而变化
['fine', 'hello', 'how', 'are', 'you']
>>> list3                   #copy()未随list1插入而变化
['hello', 'how', 'are', 'you']
>>> list1.pop(1)
'hello'
>>> list1
['fine', 'how', 'are', 'you']
>>> list2                   #'='随着list1删除而变化
['fine', 'how', 'are', 'you']
>>> list3                   #copy()未随list1删除而变化
['hello', 'how', 'are', 'you']
>>> 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值