python包含的内容_python 发现列表包含指定内容则退出循环

匿名用户

1级

2012-06-08 回答

#!/user/bin/python

def test():

while(True):

flag=0

#python 2.7

value=raw_input("Enter value:")

#python 3.2

#value=input("Enter value:")

print(value)

f1=open(r"c:\b.txt",'r').readlines()

#delete '\n' at the end of line

f1=list(map(lambda x:x.strip(),f1))

#check out the data in b.txt

print(f1)

for member in f1:

if(value in member):

print("find it!")

flag=1

break

if(flag==0):

print("Not find it, and you are free now!")

break

test()

追问:

不好意思 没把问题写清楚,你的代码我试了,要完全匹配才能判断是不是存在。

追答:

#!/user/bin/python

import sys

def test():

while(True):

flag=0

if (sys.version

#python version less than 3.0.0

value=raw_input("Enter value:")

print("this is "+sys.version)

else:

#python version greater than 3.0.0

value=input("Enter value:")

print("this is 3.0+")

f1=open(r"c:\b.txt",'r').readlines()

#delete '\n' at the end of line

f1=list(map(lambda x:x.strip(),f1))

#check out the data in b.txt

print(f1)

for member in f1:

if(value in member):

print("find it!")

flag=1

break

if(flag==0):

print("Not find it, and you are free now!")

break

test()

追问:

测试正确 谢谢你的帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值