假设我有一个类定义为:
case class Box[A](a: A)
Run Code Online (Sandbox Code Playgroud)
我想编写一个通用方法,(Box[A1](a1), .., Box[An](an))将元组解包为(a1, .., an)类型为 的元组(A1, .., An)。
我试过Match Types没有运气:
scala> type Unpack[Bs <: Tuple] <: Tuple = Bs match {
| case Unit => Unit
| case Box[a] *: bs => a *: Unpack[bs]
| }
scala> def unpack[Bs <: Tuple](bs: Bs): Unpack[Bs] = bs match {
| case () => ()
| case Box(a) *: as => a *: unpack(as)
| }
2 | case …Run Code Online (Sandbox Code Playgroud) python3.9之后,至少有三种方式使用输入提示。以内置集合set为例:
# way 1
def f(s: set[str]) -> str:
pass
# way 2
def f(s: typing.Set[str]) -> str:
pass
# way 3
def f(s: collections.abc.Set[str]) -> str:
pass
# way 3': or arguable collections.abc.MutableSet vs collections.abc.Set
def f(s: collections.abc.MutableSet[str]) -> str:
pass
Run Code Online (Sandbox Code Playgroud)
typing此外,对于某些抽象类型,和中有两个版本collections.abc,Mapping例如:
def f(m: typing.Mapping[str, int]) -> int:
pass
def f(m: collections.abc.Mapping[str, int]) -> int:
pass
Run Code Online (Sandbox Code Playgroud)
根据Python 之禅:
应该有一种——最好只有一种——明显的方法来做到这一点。
我很困惑哪一个最好?
以下代码来自Math.javajava标准库:
public static double abs(double a) {
return (a <= 0.0D) ? 0.0D - a : a;
}
Run Code Online (Sandbox Code Playgroud)
我有以下问题:
return (a <= 0) ? 0.0D - a : a;return (a <= 0) ? - a : a;谢谢你的帮助!
我想从其部分的xml 文件Component中解析数据:
<Component>\n <UnderlyingSecurityID>300001</UnderlyingSecurityID>\n <UnderlyingSecurityIDSource>102</UnderlyingSecurityIDSource>\n <UnderlyingSymbol>\xe7\x89\xb9\xe9\x94\x90\xe5\xbe\xb7</UnderlyingSymbol>\n <ComponentShare>300.00</ComponentShare>\n <SubstituteFlag>1</SubstituteFlag>\n <PremiumRatio>0.25000</PremiumRatio>\n <CreationCashSubstitute>0.0000</CreationCashSubstitute>\n <RedemptionCashSubstitute>0.0000</RedemptionCashSubstitute>\n</Component>\n<Component>\n <UnderlyingSecurityID>300003</UnderlyingSecurityID>\n <UnderlyingSecurityIDSource>102</UnderlyingSecurityIDSource>\n <UnderlyingSymbol>\xe4\xb9\x90\xe6\x99\xae\xe5\x8c\xbb\xe7\x96\x97</UnderlyingSymbol>\n <ComponentShare>600.00</ComponentShare>\n <SubstituteFlag>1</SubstituteFlag>\n <PremiumRatio>0.25000</PremiumRatio>\n <CreationCashSubstitute>0.0000</CreationCashSubstitute>\n <RedemptionCashSubstitute>0.0000</RedemptionCashSubstitute>\n</Component>\nRun Code Online (Sandbox Code Playgroud)\n我已经安装了最新版本的 lxml 和 pandas,尝试了以下代码但没有成功。
\nPython 3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)]\nType \'copyright\', \'credits\' or \'license\' for more information\nIPython 7.25.0 -- An enhanced Interactive Python. Type \'?\' for help.\n\nIn [1]: import pandas as pd\n\nIn [2]: pd.__version__\nOut[2]: \'1.3.0\'\n\nIn [3]: xml = pd.read_xml(\'https://www.huaan.com.cn/etf/159949/etffiledownload.jsp?etffilename=pcf_159949_20210707.xml\', xpath=\'//component\')\n---------------------------------------------------------------------------\nValueError Traceback (most recent call last)\n<ipython-input-3-67d228028cc9> in <module>\n----> 1 xml …Run Code Online (Sandbox Code Playgroud)