在Python中使用itertools时出错 [英] Getting error while using itertools in Python

查看:159
本文介绍了在Python中使用itertools时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 OP1 具体来说,目标是如果多个dict具有与键paper_title相同的内容,则删除重复项.

Specifically, the objective is to remove duplicates if more than one dict has the same content for the key paper_title.

但是,如果估算list的方式不一致,则该行将引发错误,例如,如果存在dictstr

However, the line throw an error if there inconsistency in the way the list is imputed, such that if there is a combination of dict and str

TypeError:字符串索引必须为整数

TypeError: string indices must be integers

产生上述错误的完整代码如下:-

The complete code which generates the aforementioned error is as below: -

from itertools import groupby



def extract_secondary():
    # 
    test_list = [{"paper_title": 'This is duplicate', 'Paper_year': 2}, \
                 {"paper_title": 'This is duplicate', 'Paper_year': 3}, \
                 {"paper_title": 'Unique One', 'Paper_year': 3}, \
                 {"paper_title": 'Unique two', 'Paper_year': 3}, 'all_result']
    f = lambda x: x["paper_title"]
    already_removed = [next(g) for k, g in groupby(sorted(test_list, key=f), key=f)]


extract_secondary()

我可以知道代码的哪一部分需要进一步调整吗?感谢任何见识.

May I know which part of the code needs further tweaks? Appreciate any insight.

PS:如果将此线程视为与 OP1 重复,请通知我.但是,我认为由于该问题的独特性,该线程应有其自身的存在.

PS: Please notify me if this thread is being considered duplicate to OP1. However, I believe this thread merits its own existence due to the uniqueness of the issue.

推荐答案

感谢@Chris指出test_lis t中存在str而不是dict ("all_result")

Thanks to @Chris for pointing about the existence of str in test_list instead of dict ("all_result")

要解决sorted引起的错误,即它不能将f用作str,则需要从列表中删除str.

To address whereby sorted is raise an error that it cannot use f for str, the str need to be removed from the list.

OP 开始,可以通过以下方式删除str:

As of OP, the str can be removed by

list(filter('all_result'.__ne__, test_list))

请注意,在这种情况下,str仅具有值'all_result'.

Note that, for this case, the str only have the value of 'all_result'.

然后是完整的代码

def extract_secondary():

        test_list = [{"paper_title": 'This is duplicate', 'Paper_year': 2}, \
                     {"paper_title": 'This is duplicate', 'Paper_year': 3}, \
                     {"paper_title": 'Unique One', 'Paper_year': 3}, \
                     {"paper_title": 'Unique two', 'Paper_year': 3},'all_result','all_result']
        test_list=list(filter('all_result'.__ne__, test_list))
        f = lambda x: x["paper_title"]
        already_removed = [next(g) for k, g in groupby(sorted(test_list, key=f), key=f)]

extract_secondary()

这篇关于在Python中使用itertools时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆