列表和字符串拓展

判断列表中元素示例

#序列类型容器,可以判断里面的元素

list1 = [321,354,764,5,87,3,0,25];
if(0 in list1):
    print("里面有0存在");
else:
    print("里面没有0存在");

list1 = ["321","354","764","5","87","3","0","25"];
if("0" in list1):
    print("里面有0存在");
else:
    print("里面没有0存在");

list1 = "423423";
if("0" in list1):
    print("里面有0存在");
else:
    print("里面没有0存在");

t1 = [1,2,3,4,511];
if([1,2] in t1):
    print("里面有[1,2]存在");
else:
    print("里面没有[1,2]存在");

if([1,2] in [[1,2],3,5,3]):
    print("里面有[1,2]存在");
else:
    print("里面没有[1,2]存在");

字符串(lower,count,find)

#字符串

s = "John Smith";
print(s);
print("使用小写函数后",s.lower());

#find函数
print("'hn'在第",s.find("hn"),"位");
print("'handsome'在第",s.find("handsome"),"位");

#或者用 in
if("hn" in s):
    print("'hn'在第", s.find("hn"), "位");
else:
    print("'hn'不在s内");

#find函数更多应用,最好别再列表上面用(这里例子不大好,可以拿字符串试试)
s = [32,453,643,7,0,423,45,0,422,654,4,43,0,53];
count = s.count(0);
s1 = str(s);
print(s1);
n=0;
for i in range(count):

    print("0在列表中第",s1.find('0',n));     # 这里的n表示从第n位开始查找
    n = s1.find('0',n) +1;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值