lists-列表操作
列表索引的某个项目可以重新赋值
举例:
nums = [7, 7, 7, 7, 7]
nums[2] = 5
print(nums)
Result:
>>>
[7, 7, 5, 7, 7]
>>>
list-列表乘&加
列表list 可以添加也可以相乘,字符串也一样
举个栗子:
nums = [1, 2, 3]
print(nums + [4, 5, 6])
print(nums * 3)
结果:
>>>
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>>
列表和字符串在很多方面都是相似的——字符串可以被看作是无法更改的字符列表。
List-检测-in
用in语句来检查列表中是否有某个项目,如果返回 True 则有,返回False则没有
words = ["spam", "egg", "spam", "sausage"]
print("spam" in words)
print("egg" in words)
print("tomato" in words)
结果:
>>>
True
True
False
>>>
in运算符还用于确定字符串是否是另一个字符串的子字符串。
List-检测-not
not语句可以检查某个元素不在列表里。如下:
nums = [1, 2, 3]
print(not 4 in nums)
print(4 not in nums)
print(not 3 in nums)
print(3 not in nums)
结果:
>>>
True
True
False
False
>>>