杂记

python中的容器:

1.list

2.tuple:只读数组

3.set:没有重复元素的数组

4.dict:字典(类似于哈希表)

5.数组切片

切片是复制

6.字符串与数组之间的关系

字符串不能直接去修改,修改字符串是要通过数组进行修改。

>>> str = 'adsfe'
>>> print(type(str))
<class 'str'>
>>> str[0] = 'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> li = list(str)
>>> print(type(li))
<class 'list'>
>>> li[0] = 'x'
>>> s = ''.join(li)
>>> print(type(s))
<class 'str'>

字符串也可和数组一样,进行分片

>>> s = 'abc,def,ghi'
>>> s.split(',')
['abc', 'def', 'ghi']
>>> p1,p2,p3 = s.split(',')
>>> print(p1,p2,p3)
abc def ghi
>>> s = 'abcdefghi'
>>> print(s[0],s[-1])
a i
>>> print(s[1:3])
bc
>>> print(s[1:5:2])
bd
原文地址:https://www.cnblogs.com/chenyang920/p/8040078.html