Python 如何缩短布尔表达式?或

Python 如何缩短布尔表达式?或,python,boolean-expression,Python,Boolean Expression,我不必写所有的数字,有没有办法把它缩短?如果是(0,10)就可以了,如果是(0,10000)呢 这里是否可以使用列表?在这种情况下,正则表达式非常简洁: var = raw_input() if "0" in var or "1" in var or "2" in var or "3" in var or "4" in var or "5" in var or "6" in var or "7" in var or "8" in var or "9" in var: print "ya

我不必写所有的数字,有没有办法把它缩短?如果是(0,10)就可以了,如果是(0,10000)呢


这里是否可以使用列表?

在这种情况下,正则表达式非常简洁:

var = raw_input()

if "0" in var or "1" in var or "2" in var or "3" in var or "4" in var or "5" in var or "6" in var or "7" in var or "8" in var or "9" in var:
    print "yay"
else:
    print: ":("
any(str(i) in var for i in range(10))
甚至更短:

import re
if re.search(r"\d", str):
  print "yay"
else
  print ":("

在这种情况下,正则表达式非常简洁:

any(str(i) in var for i in range(10))
甚至更短:

import re
if re.search(r"\d", str):
  print "yay"
else
  print ":("

根据“var”的大小和值的数量,最好使用集合

print "yay" if re.search(r"\d", str) else ":("

如果
var=raw\u input()
,那么10000个值是没有意义的,但我想您还需要考虑另一个用例。

根据“var”的大小和值的数量,使用集合可能会更好

print "yay" if re.search(r"\d", str) else ":("
如果
var=raw\u input()
,那么10000个值是没有意义的,但我想您还需要考虑另一个用例

我要确保字符串中有一个数字,然后将其转换为整数。我还需要确保字符串中没有其他simbol,而是数字

为此,您可以将
int()
应用于已读取的字符串,捕获
ValueError
异常:

values = set(map(str, range(10000)))
print(not set(var).isdisjoint(values))
我要确保字符串中有一个数字,然后将其转换为整数。我还需要确保字符串中没有其他simbol,而是数字

为此,您可以将
int()
应用于已读取的字符串,捕获
ValueError
异常:

values = set(map(str, range(10000)))
print(not set(var).isdisjoint(values))
或者,将原始输入转换为字符串:

var = raw_input()
list_of_strings = map(str, range(10))
if var in list_of_strings:
    print 'yay!'
else:
    print ':('
注意:我的第一个示例需要额外的步骤将数字列表转换为字符串列表。我的第二个示例与此相反,将输入从字符串转换为数字

或者,将原始输入转换为字符串:

var = raw_input()
list_of_strings = map(str, range(10))
if var in list_of_strings:
    print 'yay!'
else:
    print ':('

注意:我的第一个示例需要额外的步骤将数字列表转换为字符串列表。我的第二个示例与此相反,它将输入从字符串转换为数字。

最好的答案当然是

try:
    var = int(raw_input())
except ValueError as e:
    var = int(raw_input('Please enter a number!'))
if var in range(10):
    print 'yay'
else:
    print ':('
任何人都很容易理解为什么

编辑1 不,这不是最好的答案,因为它是下列方法中最慢的一种。
我喜欢正则表达式,但我无法想象使用正则表达式的解决方案会是最快的解决方案。
甚至set()的使用速度也更快

print 'yay' if any(c in '0123456789' for c in var) else ':('
结果

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
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
编辑2 天哪
事实上,申盛的答案是最好的。
与我想象的恰恰相反

0.157774521622
0.0335822010898
0.0178648403638
0.00936152499829
结果

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
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
我的结论是,通过
挖掘var
var的探索确实是超快速的。
我只知道它非常快

编辑3 没有人指出深造解决方案的执行时间取决于分析字符串的内容:

0.00467852757823
给出结果

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 
在最坏的情况下,使用ompiled regex(需要0.00936152499829秒)似乎比shensei的解决方案快。但事实上,如果编译正则表达式的时间包含在时间度量中,则实际执行时间为0.0216940979929秒。

那么申赛的解决方案仍然是最快的方法。

最好的答案当然是

try:
    var = int(raw_input())
except ValueError as e:
    var = int(raw_input('Please enter a number!'))
if var in range(10):
    print 'yay'
else:
    print ':('
任何人都很容易理解为什么

编辑1 不,这不是最好的答案,因为它是下列方法中最慢的一种。
我喜欢正则表达式,但我无法想象使用正则表达式的解决方案会是最快的解决方案。
甚至set()的使用速度也更快

print 'yay' if any(c in '0123456789' for c in var) else ':('
结果

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
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
编辑2 天哪
事实上,申盛的答案是最好的。
与我想象的恰恰相反

0.157774521622
0.0335822010898
0.0178648403638
0.00936152499829
结果

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
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
我的结论是,通过
挖掘var
var的探索确实是超快速的。
我只知道它非常快

编辑3 没有人指出深造解决方案的执行时间取决于分析字符串的内容:

0.00467852757823
给出结果

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 
在最坏的情况下,使用ompiled regex(需要0.00936152499829秒)似乎比shensei的解决方案快。但事实上,如果编译正则表达式的时间包含在时间度量中,则实际执行时间为0.0216940979929秒。
然后申赛的解决方案仍然是最快的方法。

来自O.p.评论:“我想确保字符串中有一个数字,然后将其转换为整数。此外,我需要确保字符串中没有其他simbol,而是数字–”

这在Python中很简单: 只需执行“
var.isdigit()
”-.isdigit是一个字符串方法

不过,从字符串中提取数字的推荐方法是:

0.0035278226702
0.0132472143806
在O.p.注释中:“我想确保字符串中有一个数字,然后将其转换为整数。此外,我还需要确保字符串中没有其他simbol,而是数字——”

这在Python中很简单: 只需执行“
var.isdigit()
”-.isdigit是一个字符串方法

不过,从字符串中提取数字的推荐方法是:

0.0035278226702
0.0132472143806

如果您要通过检查10000个硬编码子字符串来告诉我们您试图实现的目标,您可能会得到更有用的答案。您确定您的意思不是
如果var==“0”或var==“1”
等?你真的是说“var中有数字吗?”@JohnMachin是的,我的意思是“var中有数字吗”@我正在写一个简单的程序来计算三角形的第三个角,如果给定两个角的话。我想确定字符串中有一个数字,然后把它转换成整数。我还需要确定字符串中没有其他simbol,而是数字。你的问题是一个很清楚的例子:)如果你告诉我们,你可能会得到更多有用的答案通过检查10000个硬编码子字符串,您试图实现的目标。如果var==“0”或var==“1”等,您确定您的意思不是
?你的意思是“var中的任何地方都有数字吗?”@JohnMachin是的,我的意思是“var中的任何地方都有数字吗”@aix我正在写一个简单的程序来计算三角形的第三个角,如果给定两个角的话。我想确定字符串中有一个数字,然后转换它