Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用.net的SortedDictionary(从C#.dll导入)_Python_.net_Dictionary_Python.net - Fatal编程技术网

Python 使用.net的SortedDictionary(从C#.dll导入)

Python 使用.net的SortedDictionary(从C#.dll导入),python,.net,dictionary,python.net,Python,.net,Dictionary,Python.net,我目前正在从事一个与C#.dll交互的python(for.NET)项目。但是,我导入的SortedDictionary有点问题 这就是我正在做的: import clr from System.Collections.Generic import SortedDictionary sorted_dict = SortedDictionary<int, bool>(1, True) sorted_dict不允许我调用接口中看到的任何公共成员函数(Add、Clear、Contains

我目前正在从事一个与C#.dll交互的python(for.NET)项目。但是,我导入的SortedDictionary有点问题

这就是我正在做的:

import clr
from System.Collections.Generic import SortedDictionary
sorted_dict = SortedDictionary<int, bool>(1, True)
sorted_dict不允许我调用接口中看到的任何公共成员函数(Add、Clear、ContainsKey等)。我这样做正确吗?

“在这种情况下,这肯定是一个语法问题。您使用的是C#语法,Python解释器没有压缩。根据我刚刚找到的一些编码示例,“@martineau

问题在于:

SortedDictionary<int, bool>(1, True)
这些表达式之间的逗号将结果转换为元组,因此得到的结果是
(True,True)
。(Python2.x允许您比较任何内容;结果可能没有任何合理的含义,这里就是这样。)

显然,Python对于泛型类型使用的
语法与C#不同。而是使用
[…]

sorted_dict = SortedDictionary[int, bool](1, True)
这仍然不起作用:你得到:

TypeError: expected IDictionary[int, bool], got int
这是因为您试图用两个参数实例化该类,而该类需要一个具有字典接口的参数。因此,这将起作用:

sorted_dict = SortedDictionary[int, bool]({1: True})

编辑:我最初以为您使用的是IronPython。看起来Python for.NET使用了类似的方法,所以我认为上面的方法应该仍然有效。

您使用的是什么风格的Python解释器?我这样问是因为如果
SortedDictionary
是一个类的名称,
SortedDictionary(1,True)
会产生元组
(True,True)
。这也可以解释您得到的
属性错误。该语句不是有效的Python语法。@martineau解释器是CPython。我正在处理的项目使用SortedDictionary,但我用bool替换了这个问题的Object,因为在这种情况下它仍然不起作用。我假设这是正确的Python语法,因为我没有得到任何关于这一点的错误消息,但我认为这应该解决这个问题。谢谢@martineau你介意解释为什么SortedDictionary(1,True)会返回元组(True,True)吗?这是IronPython。@martineau非常感谢!
TypeError: expected IDictionary[int, bool], got int
sorted_dict = SortedDictionary[int, bool]({1: True})