python 模糊查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 模糊查询相关的知识,希望对你有一定的参考价值。
list =['a','aa','aaab','aaaa','cca']
我想模糊查询出来以a开头的项,如何才能做到
使用语言是python
根据你的内容我提供一段代码
for str in list:
if str.startswith('a'):
print "found it!",str
楼主测试下哈,我用的是2.x。
有问题再联系我~~蟒蛇小组祝您编程愉快! 参考技术A print [x for x in list if x.find('a')==0] 参考技术B list2 = [x for x in list if str(x).startswith('a')] 参考技术C 显然用正则表达式:
import re
re0=re.compile(r'^a')
list=['a','aa','aaab','aaaa','cca']
for i in list:
if re0.match(i):
print i 参考技术D 正则表达式
python实现登录查询(可以模糊查询)
# -*- coding: utf-8 -*- while 1: name= raw_input("Please input your name:") if name == "zhangsan": password = raw_input("Please input your password:") while password != "zhangsan": password = raw_input("password error,Please input your password again:") else: print "Login successful." print "welcome to search address list." while 1: match_yes = 0 search = raw_input("please input name whom you want to search.") contact_file = file(‘address_list.txt‘) while 1: line = contact_file.readline() if len(line) == 0:break if search in line: print ‘Congratulations:\n\n %s‘ % line match_yes = 1 if match_yes == 0: print "Sorry,not found" else: print "username error,try again."
查询的表:声明。电话号都是随便打的:
执行结果:
本文出自 “天道酬勤” 博客,请务必保留此出处http://taindaochouqin.blog.51cto.com/12995943/1947894
以上是关于python 模糊查询的主要内容,如果未能解决你的问题,请参考以下文章