当在Python编程,我现在避免map
,lambda
并filter
通过使用列表理解,因为它是在执行中更容易阅读和更快.但也reduce
可以替换?
例如,对象具有union()
在另一个对象上工作的运算符a1.union(a2)
,并且给出相同类型的第三个对象.
我有一个对象列表:
L = [a1, a2, a3, ...]
Run Code Online (Sandbox Code Playgroud)
如何将所有这些对象的union()与列表推导相对应,相当于:
result = reduce(lambda a, b :a.union(b), L[1:], L[0])
Run Code Online (Sandbox Code Playgroud) 我有一个本地DTD文件test.dtd.内容是:
<!DOCTYPE coord [
<!ELEMENT coord (date)>
<!ELEMENT date (#PCDATA)>
]>
Run Code Online (Sandbox Code Playgroud)
我想使用xmllint验证XML.此XML中没有DOCTYPE:
<?xml version="1.0" encoding="x-mac-roman"?>
<coord>
<date>20150312</date>
</coord>
Run Code Online (Sandbox Code Playgroud)
如果我将DTD块作为第二行插入到我的XML文件的副本中并使用:
xmllint --valid --noout my2.xml
Run Code Online (Sandbox Code Playgroud)
但是当我尝试:
xmllint --loaddtd test.dtd --valid --noout my.xml
xmllint --dtdvalid test.dtd --noout my.xml
Run Code Online (Sandbox Code Playgroud)
两者都不起作用.结果是:
test.dtd:1: parser error : Content error in the external subset
<!DOCTYPE coord [
^
test.dtd:1: parser error : Content error in the external subset
<!DOCTYPE coord [
^
Could not parse DTD test.dtd
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?看来我的XML 必须包含一个DOCTYPE行(带有SYSTEM关键字)来引用我想要避免的外部DOCTYPE文件.请参阅:http://www.w3schools.com/dtd/
有没有修改XML的解决方案?
我知道如何使用python通过最小二乘法解决AX = B:
例:
A=[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,0,0]]
B=[1,1,1,1,1]
X=numpy.linalg.lstsq(A, B)
print X[0]
# [ 5.00000000e-01 5.00000000e-01 -1.66533454e-16 -1.11022302e-16]
Run Code Online (Sandbox Code Playgroud)
但是如果用权重矩阵不是身份来解决这个相同的等式呢:
A.X = B (W)
Run Code Online (Sandbox Code Playgroud)
例:
A=[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,0,0]]
B=[1,1,1,1,1]
W=[1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
谢谢提前,
如果我想在第一个组引用后插入"0",那么语法是什么?
import re
re.sub("(..)(..)", "\\1x\\2", "toto")
toxto
re.sub("(..)(..)", "\\10\\2", "toto")
sre_constants.error: invalid group reference
Run Code Online (Sandbox Code Playgroud)
错误,因为\ 10被解释为第10个参考组(这就是为什么在ed()中,组引用在[1-9]间隔中).
在上面的例子中,如何获得"to0to"?
在python中,我有一个普通的“外部”多边形和一个“内部”多边形列表。我想使用此列表在多边形中打孔。
from shapely.geometry import Polygon
# polygon with 1 hole in the middle
p = Polygon(((0,0),(10,0),(10,10),(0,10)), (((4,4),(4,6),(6,6),(6,4)), ))
print p.wkt
# POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (4 4, 4 6, 6 6, 6 4, 4 4))
# other constructor, does not work (no hole) :
outer = Polygon(((0,0),(10,0),(10,10),(0,10),(0,0)))
inners = (Polygon(((4,4),(4,6),(6,6),(6,4),(4,4))), )
p = Polygon(outer, inners)
print p.wkt
# POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))
Run Code Online (Sandbox Code Playgroud)
如何根据给定的外部和内部构造p?
我希望计算一个简单的校验和:只需添加所有字节的值.
我发现最快的方法是:
checksum = sum([ord(c) for c in buf])
Run Code Online (Sandbox Code Playgroud)
但对于13 Mb数据buf,需要4.4 s:太长(在C中,需要0.5 s)
如果我使用:
checksum = zlib.adler32(buf) & 0xffffffff
Run Code Online (Sandbox Code Playgroud)
需要0.8秒,但结果不是我想要的.
所以我的问题是:是否有任何函数,或者包含在python 2.6中的lib或C来计算一个简单的校验和?
谢谢提前,埃里克.
在关于扩展序列的所有示例中,所有新元素都在末尾附加.见personinfo
和fullpersoninfo
在:
http://www.w3schools.com/schema/schema_complex.asp
如何通过扩展定义新序列以插入新元素?示例(第二部分是错误的;如何纠正?):
<xs:complexType name="address">
<xs:sequence>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
<xs:extension base="address"/>
</xs:complexContent>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
目的是验证一些元素在许多序列的位置city
和country
结尾.
示例:
<Employee>
<name>A.Miller</name>
<city>Madrid</city>
<country>Spain</country>
</Employee>
<Flight>
<airport>CDG</airport>
<city>Paris</city>
<country>France</country>
</Flight>
Run Code Online (Sandbox Code Playgroud) 我通常使用带有""的多行文档字符串来评论我的函数,如下所述:https: //www.python.org/dev/peps/pep-0257/
def func1(x):
"""
This function does ...
"""
...
Run Code Online (Sandbox Code Playgroud)
但是评论lambda函数的最佳方法是什么?我在犹豫之间犹豫:
# This function does ...
func2 = lambda x: ...
Run Code Online (Sandbox Code Playgroud)
要么 :
func2 = lambda x: ...
""" This function does ... """
Run Code Online (Sandbox Code Playgroud)
要不然 ?
我有一个大的 XML 文件,它的结构大致如下(按此顺序):
<document>
<interesting_part>
...
</interesting_part>
<foo>
...
60000 lines
...
</foo>
</document>
Run Code Online (Sandbox Code Playgroud)
我的程序是:
from xml.etree import ElementTree as et
f=open(path_f)
tree=et.parse(f)
f.close()
# retreive infos from tree...
Run Code Online (Sandbox Code Playgroud)
我只对文件中的前几个块感兴趣,但性能很低,因为 et.parse() 加载了整个文件。
如何只加载文件直到</interesting_part>?
我想到了这样的事情:
class My_Parser(et.XMLParser):
????
my_parser = My_Parser()
tree=et.parse(f, my_parser)
Run Code Online (Sandbox Code Playgroud)
提前谢谢你,埃里克。
使用 python logging 包,并编写一个类 Log,我想将 stdout 和 stderr 发送到日志文件:
log = Log("log.txt")
print "line1"
print "line2"
print >>sys.stderr, "err1"
del log
print "line to screen only"
Run Code Online (Sandbox Code Playgroud)
输出日志文件将包含:
16/11/2017 09:51:58 INFO - line1
16/11/2017 09:51:58 INFO - line2
16/11/2017 09:51:58 INFO - err1
Run Code Online (Sandbox Code Playgroud)
知道如何编写这个 Log 类,保持“日志”包(时间戳,...)的优势吗?
有时,在一个类中,我想__init__
,当传递给它时,例如错误的参数,不要实例化对象.解决方案可能是引发异常(哪一个?).还有其他想法吗?
python ×9
xml ×2
byte ×1
checksum ×1
constructor ×1
docstring ×1
dtd ×1
elementtree ×1
file ×1
init ×1
lambda ×1
list ×1
local ×1
logging ×1
map-function ×1
matrix ×1
numpy ×1
ord ×1
parsing ×1
partial ×1
performance ×1
polygon ×1
reduce ×1
regex ×1
shapely ×1
stdout ×1
sum ×1
tee ×1
xmllint ×1
xsd ×1