我有这样定义的函数:
def f1 (a, b, c = None, d = None):
.....
Run Code Online (Sandbox Code Playgroud)
我如何检查a,b不等于某个值.我想检查它们不是空字符串,""或者" "
思考像.
arguments = locals()
for item in arguments:
check_attribute(item, arguments[item])
Run Code Online (Sandbox Code Playgroud)
然后检查参数是否不是""," ".但在这种情况下,它也会尝试检查None值(我不想做的事情).
假设我有一小段代码:
<?php
$tmp = str_split('hello world!',2);
// $tmp will now be: array('he','ll','o ','wo','rl','d!');
foreach($tmp AS &$a) {
// some processing
}
unset($tmp);
?>
Run Code Online (Sandbox Code Playgroud)
我怎么能在Python v2.7中做到这一点?
我以为这样做会:
the_string = 'hello world!'
tmp = the_string.split('',2)
for a in tmp:
# some processing
del tmp
Run Code Online (Sandbox Code Playgroud)
但它返回一个"空分隔符"错误.
有什么想法吗?
以下正则表达式是错误的 - 有人可以帮助找到所有:-)前面没有"请忽略我"吗?我之前不需要这样的正则表达式.单词边界可能会混淆事物.
谢谢
<script type="text/javascript">
function regexTest(string, assert) {
document.write('<hr>')
document.write(string)
document.write("[")
document.write(string.match(/\b((?!please ignore me )\:\-\))\b/gi)!=null);
document.write("]?" + assert);
}
regexTest("1. this is the first string :-) ",true);
regexTest("2. :-)",true)
regexTest("3. another string:-)here",true);
regexTest("4. Ending a sentence with :-)",true);
regexTest("5. please ignore me :-)",false);
</script>
Run Code Online (Sandbox Code Playgroud) 我试图将魔术方法装饰__getitem__成课堂上的类方法.这是我尝试的样本.我不介意使用classmethod或staticmethod装饰,但我不太清楚如何做到这一点.这是我尝试过的:
import ConfigParser
class Settings(object):
_env = None
_config = None
def __init__(self, env='dev'):
_env = env
# find the file
filePath = "C:\\temp\\app.config"
#load the file
_config = ConfigParser.ConfigParser()
_config.read(filePath)
@classmethod
def __getitem__(cls, key):
return cls._config.get(cls._env, key)
@classmethod
def loadEnv(cls, env):
cls._env = env
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试调用时,Settings['database']我收到以下错误.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected Array[Type], got str
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我做错了什么.此外,有人可以建议是否有更好的方法来做到这一点?我甚至尝试过使用MetaClasses,但收效甚微(因为我不太了解python).
class Meta(type):
def __getitem__(*args):
return type.__getitem__(*args)
class Settings(object):
__metaclass__ = Meta
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我正在尝试将变量转换为数组并使用PHP拆分每个字符.
所以说我有一个变量$name = 'John Smith';如何将其转换为:
array('J','o','h','n',' ','S','m','i','t','h');
Run Code Online (Sandbox Code Playgroud)
注意John和Smith之间的空间.
谢谢.
在计算时
math.factorial(100)
Run Code Online (Sandbox Code Playgroud)
我明白了:
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L
为什么数字末尾有一个L?
如何self.value在函数定义中调用 a ?
class toto :
def __init__(self):
self.titi = "titi"
def printiti(self,titi=self.titi):
print(titi)
Run Code Online (Sandbox Code Playgroud) itemList = ["a","b","c","d","e","f","g","h"]
aa = "NULL"
bb = "NULL"
cc = "NULL"
for item in itemList:
aa = bb
bb = cc
cc = item
if aa == "NULL":
continue
print "%s_%s_%s" % (aa, bb, cc)
Run Code Online (Sandbox Code Playgroud) 下面的if语句在某处有问题,我无法弄明白.任何可能导致其无法正常运行的约定或方法滥用?checkList是用户输入的句子,lis是一个很大的单词列表.
def realCheck(checkList):
string = "".join(checkList)
print string
wordList = string.split()
if match(wordList, lis).sort(key=str.lower) == wordList.sort(key=str.lower):
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)