列表中的元素可变
>>> numbers = [17, 123]
>>> numbers[1] = 5
>>> print(numbers)
[17, 5]
python 对英文的分词(超级方便)
- 首先是列表和字符串
s = 'spam'
seperate = list(s)
print(seperate)
## output: ['s', 'p', 'a', 'm']
revert = s.casefold()
print(revert)
## output: spam
- 句子和单词
s = 'pining for the fjords'
words = s.split()
print(words)
## output: ['pining', 'for', 'the', 'fjords']
sen = ' '.join(str(word) for word in words)
print(sen)
## output: pining for the fjords
- 判断是否指同一个object,可以用is作判断。True表明同,False表不是。
>>> a = 'banana'
>>> b = 'banana'
>>> a is b
True
>>> a = [1, 2, 3]
>>> b = a
>>> b is a
True
>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a is b
False
- append表明修改的是同一张列表,+是新创建一个列表
>>> t1 = [1, 2]
>>> t2 = t1.append(3)
>>> print(t1)
[1, 2, 3]
>>> print(t2)
None
>>> t3 = t1 + [3]
>>> print(t3)
[1, 2, 3]
>>> t2 is t3
False