应用列表函数

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

>>>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值