Лучший ответ, конечно,
print 'yay' if any(c in '0123456789' for c in var) else ':('
любой легко поймет, почему
редактировать 1
Нет, это не самый лучший ответ, потому что он самый медленный из следующих методов.
Я люблю регулярные выражения, но я не мог себе представить, что решение с использованием регулярных выражений будет самым быстрым.
Даже использование set () быстрее.
var = '''For all his fame and celebration, William Shakespeare remains a mysterious figure
with regards to personal history. There are just two primary sources for information
on the Bard: his works, and various legal and church documents that have survived from
Elizabethan times. Naturally, there are many gaps in this body of information, which
tells us little about Shakespeare the man.
William Shakespeare was born in Stratford-upon-Avon, allegedly on April 23, 1564.'''
from time import clock
import re
n = 1000
te = clock()
for i in xrange(n):
b = any(c in ('0123456789') for c in var)
print clock()-te
ss = set('0123456789')
te = clock()
for i in xrange(n):
b = ss.intersection(var)
print clock()-te
te = clock()
for i in xrange(n):
b = re.search('\d',var)
print clock()-te
regx = re.compile('\d')
te = clock()
for i in xrange(n):
b = regx.search(var)
print clock()-te
результат
0.157774521622
0.0335822010898
0.0178648403638
0.00936152499829
редактировать 2
По Jove!
shensei на самом деле лучший ответ.
Просто вопреки тому, что я себе представлял!
from time import clock
import re
n = 1000
te = clock()
for i in xrange(n):
b = any(dig in var for dig in '0123456789')
print clock()-te
результат
0.00467852757823
Я пришел к выводу, что исследование var на for dig in var
действительно супер-гипер-быстрое.
Я только знал, что это было очень быстро.
редактировать 3
Никто не указал, что время выполнения решения Шенсея зависит от содержания анализируемой строки:
from time import clock
n = 1000
var = '''For all his fame and celebration, William Shakespeare remains a mysterious figure
with regards to personal history. There are just two primary sources for information
on the Bard: his works, and various legal and church documents that have survived from
Elizabethan times. Naturally, there are many gaps in this body of information, which
tells us little about Shakespeare the man.
William Shakespeare was born in Stratford-upon-Avon, allegedly on April 00, 0000.'''
te = clock()
for i in xrange(n):
b = any(dig in var for dig in '0123456789')
print clock()-te
var = '''For all his fame and celebration, William Shakespeare remains a mysterious figure
with regards to personal history. There are just two primary sources for information
on the Bard: his works, and various legal and church documents that have survived from
Elizabethan times. Naturally, there are many gaps in this body of information, which
tells us little about Shakespeare the man.
William Shakespeare was born in Stratford-upon-Avon, allegedly on April 99, 9999.'''
te = clock()
for i in xrange(n):
b = any(dig in var for dig in '0123456789')
print clock()-te
дает результаты
0.0035278226702
0.0132472143806
Использование скомпилированного регулярного выражения, которое занимает 0,00936152499829 секунд, в худшем случае кажется более быстрым, чем решение Шенсей. Но на самом деле, если время компиляции регулярного выражения включено в измерение времени, реальное время выполнения составляет 0,0216940979929 секунд.
Тогда решение Шенсея остается самым быстрым методом.