【问题标题】:Delete multiple object inside list with pop [duplicate]用pop删除列表中的多个对象[重复]
【发布时间】:2014-06-14 23:40:08
【问题描述】:

我有一个 JSON 对象,其中包含一些随机重复的属性。我想根据"word" index 删除那些重复的,只保留第一次出现,如示例所示:

{ "word" : "Apple", "meaning" : "First meaning" },
{ "word" : "Ball", "meaning" : " \u090f\u0909\u091f\u093e" },
{ "word" : "Cat", "meaning" : " \u090f\u0909\u091f\u093e" },
{ "word" : "Apple", "meaning" : "Repeated, but has another meaning" },
{ "word" : "Doll", "meaning" : " \u090f\u0909\u091f\u093e" },

我是一名 Python 初学者,目前无法领先于这个解决方案:

#!/usr/bin
import json

source="/var/www/dictionary/repeated.json"
destination="/var/www/dictionary/corrected.json"

def remove_redundant():

    with open(source, "r") as src:      
        src_object = json.load(src)

        for i in xrange(len(src_object)):

            escape = 1

            for j in xrange(len(src_object)):

                if src_object[j]["word"] == src_object[i]["word"]:

                    # leave the first occurance
                    if escape == 1:
                        escape = 2
                        continue
                    else:
                        src_object.pop(j)

    # open(destination, "w+").write(json.dumps(src_object, sort_keys=True, indent=4, separators=(',', ': ')))

    src.close()

remove_redundant()

我不断收到的错误是IndexError: list index out of range,因为 len 不断变化。感谢您的帮助。

【问题讨论】:

  • 这是一个字典列表吗?

标签: python


【解决方案1】:

这里是一个使用pop()的示例供参考

a = [{ "word" : "Apple", "meaning" : "First meaning" },
     { "word" : "Ball", "meaning" : " \u090f\u0909\u091f\u093e" },
     { "word" : "Cat", "meaning" : " \u090f\u0909\u091f\u093e" },
     { "word" : "Apple", "meaning" : "Repeated, but has another meaning" },
     { "word" : "Doll", "meaning" : " \u090f\u0909\u091f\u093e" },]

b = list()
keys = set()

while a:
    x = a.pop(0)
    if x['word'] not in keys:
        keys.add(x['word'])
        b.append(x)
a = b
del b
del keys

a 现在包含:

[{'meaning': 'First meaning', 'word': 'Apple'},
 {'meaning': ' \\u090f\\u0909\\u091f\\u093e', 'word': 'Ball'},
 {'meaning': ' \\u090f\\u0909\\u091f\\u093e', 'word': 'Cat'},
 {'meaning': ' \\u090f\\u0909\\u091f\\u093e', 'word': 'Doll'}]

【讨论】:

  • 看起来不错..但这和其他解决方案都会导致不必要的u infront 附加如下: [{u'meaning': u' \First result', u'word': u '苹果'}, {u'意思': u' \u090f\u0909\u091f\u093e', u'字': u'球'}, {u'意思': u'\u0905\u0930\u092c \u0926 \u0947\u0936\u092e\u093e \u0932
  • 不,这不是不必要的 - 它只是意味着它是 unicode。
【解决方案2】:

你可以这样做

from collections import OrderedDict
d = OrderedDict()
for item in data:
    if item["word"] not in d:
        d[item["word"]] = item

print d.values()

输出

[{'meaning': 'First meaning', 'word': 'Apple'},
 {'meaning': ' \\u090f\\u0909\\u091f\\u093e', 'word': 'Ball'},
 {'meaning': ' \\u090f\\u0909\\u091f\\u093e', 'word': 'Cat'},
 {'meaning': ' \\u090f\\u0909\\u091f\\u093e', 'word': 'Doll'}]

【讨论】:

  • 看起来不错..但这和其他解决方案都会导致不必要的 u infront 附加如下: [{u'meaning': u' \First result', u'word': u'Apple '}, {u'意思': u' \u090f\u0909\u091f\u093e', u'字': u'球'}, {u'意思': u'\u0905\u0930\u092c \u0926\u0947 \u0936\u092e\u093e \u0932.. 猜猜它与编码有关?
  • @Sushil 表示该字符串是一个unicode字符串,你不必担心。如果这真的让你感到不安,你可以使用str 函数来摆脱它:)
猜你喜欢
  • 1970-01-01
  • 2019-07-02
  • 2012-01-09
  • 2018-07-19
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
相关资源
最近更新 更多