Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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 2.7 Python TypeError:无法将字典更新序列元素#1转换为序列_Python 2.7 - Fatal编程技术网

Python 2.7 Python TypeError:无法将字典更新序列元素#1转换为序列

Python 2.7 Python TypeError:无法将字典更新序列元素#1转换为序列,python-2.7,Python 2.7,从O'Reilly的“Python for data Analysis”中的一个示例中进行一些数据争论 我们从以下格式的数据开始: In [108]: data.CATEGORY[:5] Out[108]: 0 1. Urgences | Emergency, 3. Public Health, 4 1. Urgences | Emergency, 5 5e. Communi

从O'Reilly的“Python for data Analysis”中的一个示例中进行一些数据争论

我们从以下格式的数据开始:

In [108]: data.CATEGORY[:5]
Out[108]: 
0          1. Urgences | Emergency, 3. Public Health, 
4                            1. Urgences | Emergency, 
5                       5e. Communication lines down, 
6    4. Menaces | Security Threats, 4e. Assainissem...
7                      4. Menaces | Security Threats, 
Name: CATEGORY, dtype: object
然后,本书列出了从每个条目中删除句点和“|”的步骤,目的是使用以下定义创建词典

def get_all_categories(cat_series):
    cat_sets = (set(to_cat_list(x)) for x in cat_series)
    return sorted(set.union(*cat_sets))

def get_english(cat):
    code, names = cat.split('.')
    if '|' in names:
        names = names.split(' | ')[1]
    return code, names.strip()
第一步很好,创建独特类别的列表

In [109]: all_cats = get_all_categories(data.CATEGORY)

In [110]: all_cats[:5]
Out[110]: 
['1. Urgences | Emergency',
 '1a. Highly vulnerable',
 '1b. Urgence medicale | Medical Emergency',
 '1c. Personnes prises au piege | People trapped',
 '1d. Incendie | Fire']
但是,使用第二个定义会产生以下结果:

In [116]: english_mapping = dict(get_english(x) for x in all_cats)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-116-e69c3419c341> in <module>()
----> 1 english_mapping = dict(get_english(x) for x in all_cats)

TypeError: cannot convert dictionary update sequence element #1 to a sequence
[116]中的
:english\u mapping=dict(在所有猫中为x获取英语(x))
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 english_mapping=dict(在所有猫中为x获取英语(x))
TypeError:无法将字典更新序列元素#1转换为序列
请为Python noob提供一些帮助:)

以下是解决方案:

  • “无法将字典更新序列元素#1转换为序列”-因为表达式“get_english(x)for x in all_cats”中存在空值,因此无法将其转换为字典

  • 为什么?


  • ----问题是,最后一行不应该缩进,如果不缩进,您将得到一些空值。

    在您提供的输入数据上运行时,我没有收到此错误。只是在其他计算机上再次尝试,我继续得到相同的TypeError。我忘了键入get_all_categories定义所必需的以下定义:
    def to_cat_list(catstr):stripped=(x.strip()表示catstr中的x.split(','))return[x表示stripped if x中的x]
    不确定在没有Scott…@user3334415的情况下,代码是如何工作的…@user3334415我也没有收到错误。这里的链接:警告:我抄了一条捷径,只是在直接作业中使用了你发布的
    all_cats
    (前五个元素)的摘录,但我对你加载的完整数据了解不够,不知道这是否会导致你的结果不同。
    def get_english(cat):
        code,names=cat.split('.')
        if '|' in names:
            names=names.split('|')[1]
            return code,names.strip()