我正试图想出一个断言语句,用于检查非空字符串s是否包含字母数字字符和空格:
assert s.isalnum()
我知道如果有空格,它会返回False,因为它会检查每个字符是字母还是数字。我该如何解决这个问题?
编辑:为了澄清,我试图创建一个断言语句,检查非空字符串是否包含字母数字字符和/或空格。例如,"a4bc"one_answers"ab"都应返回True。
您可以使用all来检查每个字符是字母数字还是空格:
text = "apple and 123"
result = all(c.isalnum() or c.isspace() for c in text)
print(result)
text = "with ."
result = all(c.isalnum() or c.isspace() for c in text)
print(result)
输出
True
False
如果它最多包含空格和字母数字字符,则可以执行以下操作:
def only_alnum_and_spaces(t):
counts = {"spaces" : 0, "alnums": 0}
for c in t:
if c.isalnum():
counts["alnums"] += 1
elif c.isspace():
counts["spaces"] += 1
else:
return False
return counts["alnums"] > 0 and counts["spaces"] > 0
print(only_alnum_and_spaces("apple and 123"))
print(only_alnum_and_spaces("with ."))
print(only_alnum_and_spaces("appleand123"))
输出
True
False
False
另请注意,正如@Chris_Rands所提到的,这个.ispace将选项卡视为空白。
也许这就是您想要的:
assert any(substr.issapce() or substr.isdigit() or substr.isalpha() for substr in s)
测试字符串:
>>> s1 = '123 45 abc 67 d'
>>> s2 = '123456'
>>> s3 = 'abcd'
>>> s4 = ':?--==++'
检查字符串是否包含任何空格:
>>> def hasAnySpace(str):
... return ' ' in str
...
>>> hasAnySpace(s1)
True
>>> hasAnySpace(s2)
False
>>> hasAnySpace(s3)
False
>>> hasAnySpace(s4)
False
检查字符串是否包含任何数字,可以使用任何函数和str.isdigit函数:
>>> def hasAnyDigit(str):
... return any (substr.isdigit() for substr in str)
...
>>> hasAnyDigit(s1)
True
>>> hasAnyDigit(s2)
True
>>> hasAnyDigit(s3)
False
>>> hasAnyDigit(s4)
False
检查字符串是否包含任何字母字符,可以使用任何函数和str.isalpha函数:
>>> def hasAnyAlpha(str):
... return any(substr.isalpha() for substr in str)
...
>>> hasAnyAlpha(s1)
True
>>> hasAnyAlpha(s2)
False
>>> hasAnyAlpha(s3)
True
>>> hasAnyAlpha(s4)
False
检查字符串是否包含任何数字、任何字母字符或任何空格:
>>> def hasAnyAlNumSpace(str):
... return any(substr.isalpha() or substr.isdigit() or substr.isspace() for substr in str)
...
>>> hasAnyAlNumSpace(s1)
True
>>> hasAnyAlNumSpace(s2)
True
>>> hasAnyAlNumSpace(s3)
True
>>> hasAnyAlNumSpace(s4)
False
如果您想使用assert语句,您可以使用它们的任意组合:
>>> assert hasAnySpace(s1) or hasAnyDigit(s1) or hasAnyAlpha(s1)
>>> assert hasAnySpace(s2) or hasAnyDigit(s2) or hasAnyAlpha(s2)
>>> assert hasAnySpace(s3) or hasAnyDigit(s3) or hasAnyAlpha(s3)
>>> assert hasAnySpace(s4) or hasAnyDigit(s4) or hasAnyAlpha(s4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>>
>>> assert hasAnySpace(s1)
>>> assert hasAnySpace(s2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>>
>>> assert hasAnyAlNumSpace(s1)
>>> assert hasAnyAlNumSpace(s2)
>>> assert hasAnyAlNumSpace(s3)
>>> assert hasAnyAlNumSpace(s4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
当然,如果你不喜欢这些方法,你可以简单地使用这样的断言:
assert ' ' in s1
assert any(substr.isdigit() for substr in s1)
assert any(substr.isalpha() for substr in s1)
assert (' ' in s1) or any(substr.isdigit() or substr.isalpha() for substr in s1)
assert any(substr.issapce() or substr.isdigit() or substr.isalpha() for substr in s1)
您可以删除要测试的空格:
assert s.replace(" ","").isalnum()
这将是一种
def customIsAlnum(String[] words):
if(len(words) > 1):
for(word in words):
if(!word.isalnum()):
return false
else:
return false
return true
String str = "chelsea scored 3 goals"
String[] words = str.split()
print customIsAlnum(words)