我有一个文件包含这个:
<html>
<head>
<title> Hello! - {{ today }}</title>
</head>
<body>
{{ runner_up }}
avasd
{{ blabla }}
sdvas
{{ oooo }}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
什么是提取最好的或最Python的方式{{today}},{{runner_up}}等等?
我知道它可以通过分割/正则表达式完成,但我想知道是否还有其他方法.
PS:考虑加载在一个变量中的数据thedata.
编辑:我认为HTML示例很糟糕,因为它将一些评论者指向BeautifulSoup.所以,这是一个新的输入数据:
Fix grammatical or {{spelling}} errors.
Clarify meaning without changing it.
Correct minor {{mistakes}}.
Add related resources or links.
Always respect the original {{author}}.
Run Code Online (Sandbox Code Playgroud)
输出:
spelling
mistakes
author
Run Code Online (Sandbox Code Playgroud) 我尝试了许多使用蒙特卡罗寻找π的算法.其中一个解决方案(在Python中)是这样的:
def calc_PI():
n_points = 1000000
hits = 0
for i in range(1, n_points):
x, y = uniform(0.0, 1.0), uniform(0.0, 1.0)
if (x**2 + y**2) <= 1.0:
hits += 1
print "Calc2: PI result", 4.0 * float(hits) / n_points
Run Code Online (Sandbox Code Playgroud)
可悲的是,即使有100万,精度也非常差(3.141 ......).
这是这种方法可以提供的最大精度吗?我选择蒙特卡罗的原因是它很容易在平行部分打破它.是否有另一种π算法很容易分解并计算?
为什么(例如web2py)从字典中的控制器返回数据而不是变量(参见Rails)?
例如:
return dict(sape=4139, guido=4127, jack=4098)
Run Code Online (Sandbox Code Playgroud)
而不是(这是Rails的方式)
@var1 = "jello"
@var2 = "hihi"
Run Code Online (Sandbox Code Playgroud)
使用字典而不是普通变量(速度/代码方式)是否有任何优势?
更新:上面的方法实际上是创建字典的正确方法(至少在Python 2.6.1中).另一种方式(许多人说它是正确的)
return {"var1": "jello", "var2": "hihi"}
Run Code Online (Sandbox Code Playgroud)
python框架并没有大量使用它.
从Python的文档:"当键是简单的字符串时,有时更容易使用关键字参数指定对:"
dict(sape=4139, guido=4127, jack=4098)
Run Code Online (Sandbox Code Playgroud) 我搜索并尝试覆盖"for"关键字,但我什么也没找到.我正在尝试这样的事情:
def for("maybe_arguments_go_here")
print "Hello from function for!"
end
for i in 1..3
print "Hello from for"
end
Run Code Online (Sandbox Code Playgroud)