TypeError:+:'dict_keys'和'list'不支持的操作数类型

时间:2022-09-10 22:06:06

I am trying to use a Python package called bidi. In a module in this package (algorithm.py) there are some lines that give me error, although it is part of the package.

我正在尝试使用名为bidi的Python包。在这个包中的模块(algorithm.py)中,有一些行给了我错误,尽管它是包的一部分。

Here are the lines:

以下是这些行:

_LEAST_GREATER_ODD = lambda x: (x + 1) | 1
_LEAST_GREATER_EVEN = lambda x: (x + 2) & ~1

X2_X5_MAPPINGS = {
    'RLE': (_LEAST_GREATER_ODD, 'N'),
    'LRE': (_LEAST_GREATER_EVEN, 'N'),
    'RLO': (_LEAST_GREATER_ODD, 'R'),
    'LRO': (_LEAST_GREATER_EVEN, 'L'),
}

# Added 'B' so X6 won't execute in that case and X8 will run its course
X6_IGNORED = X2_X5_MAPPINGS.keys() + ['BN', 'PDF', 'B']
X9_REMOVED = X2_X5_MAPPINGS.keys() + ['BN', 'PDF']

If I run the code in Python 3 I get this error message:

如果我在Python 3中运行代码,我会收到以下错误消息:

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    from bidi.algorithm import get_display
  File "C:\Python33\lib\site-packages\python_bidi-0.3.4-py3.3.egg\bidi\algorithm.py", line 41, in <module>
    X6_IGNORED = X2_X5_MAPPINGS.keys() + ['BN', 'PDF', 'B']
TypeError: unsupported operand type(s) for +: 'dict_keys' and 'list'

Why there is this error although this is part of the bidi package? Does it have anything to do with my Python version? I appreciate any help on this.

虽然这是bidi包的一部分,但为什么会出现此错误?它与我的Python版本有什么关系吗?我对此表示感谢。

3 个解决方案

#1


11  

In Python 3.x, dict.keys returns a dictionary view:

在Python 3.x中,dict.keys返回一个字典视图:

>>> a = {1:1, 2:2}
>>> a.keys()
dict_keys([1, 2])
>>> type(a.keys())
<class 'dict_keys'>
>>>

You can get what you want by putting those views in list:

您可以通过将这些视图放入列表中来获得所需内容:

X6_IGNORED = list(X2_X5_MAPPINGS.keys()) + ['BN', 'PDF', 'B']
X9_REMOVED = list(X2_X5_MAPPINGS.keys()) + ['BN', 'PDF']

Actually, you don't even need .keys anymore since iterating over a dictionary yields its keys:

实际上,你甚至不需要.keys,因为迭代字典产生它的键:

X6_IGNORED = list(X2_X5_MAPPINGS) + ['BN', 'PDF', 'B']
X9_REMOVED = list(X2_X5_MAPPINGS) + ['BN', 'PDF']

#2


5  

Yes, it has something to do with your Python version. In Python 2.x, dict.keys returns a list of a dictionary’s keys. In Python 3.x, it provides a view object of the keys.

是的,它与您的Python版本有关。在Python 2.x中,dict.keys返回字典键的列表。在Python 3.x中,它提供了键的视图对象。

You can call list() on the result to make it a list, or just call list() on the entire dictionary as a shortcut.

您可以在结果上调用list()使其成为列表,或者只调用整个字典上的list()作为快捷方式。

#3


2  

In Python 3.x, dict.keys doesn't return a list, but instead a view object, dict_keys.

在Python 3.x中,dict.keys不返回列表,而是返回视图对象dict_keys。

To achieve what you wanted, you need to convert it to a list:

要实现您想要的,您需要将其转换为列表:

X6_IGNORED = list(X2_X5_MAPPINGS.keys()) + ['BN', 'PDF', 'B']

#1


11  

In Python 3.x, dict.keys returns a dictionary view:

在Python 3.x中,dict.keys返回一个字典视图:

>>> a = {1:1, 2:2}
>>> a.keys()
dict_keys([1, 2])
>>> type(a.keys())
<class 'dict_keys'>
>>>

You can get what you want by putting those views in list:

您可以通过将这些视图放入列表中来获得所需内容:

X6_IGNORED = list(X2_X5_MAPPINGS.keys()) + ['BN', 'PDF', 'B']
X9_REMOVED = list(X2_X5_MAPPINGS.keys()) + ['BN', 'PDF']

Actually, you don't even need .keys anymore since iterating over a dictionary yields its keys:

实际上,你甚至不需要.keys,因为迭代字典产生它的键:

X6_IGNORED = list(X2_X5_MAPPINGS) + ['BN', 'PDF', 'B']
X9_REMOVED = list(X2_X5_MAPPINGS) + ['BN', 'PDF']

#2


5  

Yes, it has something to do with your Python version. In Python 2.x, dict.keys returns a list of a dictionary’s keys. In Python 3.x, it provides a view object of the keys.

是的,它与您的Python版本有关。在Python 2.x中,dict.keys返回字典键的列表。在Python 3.x中,它提供了键的视图对象。

You can call list() on the result to make it a list, or just call list() on the entire dictionary as a shortcut.

您可以在结果上调用list()使其成为列表,或者只调用整个字典上的list()作为快捷方式。

#3


2  

In Python 3.x, dict.keys doesn't return a list, but instead a view object, dict_keys.

在Python 3.x中,dict.keys不返回列表,而是返回视图对象dict_keys。

To achieve what you wanted, you need to convert it to a list:

要实现您想要的,您需要将其转换为列表:

X6_IGNORED = list(X2_X5_MAPPINGS.keys()) + ['BN', 'PDF', 'B']