Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

python - Python-从Json请求中删除重复项(Python - Remove Duplicates From Json Requests)

Can't find a solution to this, what i want to do is simply remove duplicates whenever i call/ request information from an api, the return is a dictionary inside a list but sometimes the list is just empty so i get jsoninfo = [] , for this i just added a check to make sure it's not empty

(找不到解决方案,我想做的就是只要我从api调用/请求信息就删除重复项,返回的是列表内的字典,但有时列表只是空的,所以我得到jsoninfo = [] ,为此,我刚刚添加了一个支票以确保它不为空)

if len(jsoninfo) != 0:

i'm also using a while loop to iterate and call the api.

(我还使用while循环来迭代并调用api。)

the problem starts when i receive duplicates of the same product for exmaple:

(当我收到相同产品的重复副本时,问题就开始了:)

jsoninfo = [{'orderID': 'ed6ae4-ae36-bab540bbba2b', 'product': 'dicals', 'cost': 150, 'Qty': 5}]
jsoninfo = [{'orderID': 'ed6ae4-ae36-bab540bbba2b', 'product': 'dicals', 'cost': 150, 'Qty': 5}]
jsoninfo = [{'orderID': 'ed6ae4-ae36-bab540bbba2b', 'product': 'dicals', 'cost': 150, 'Qty': 5}]

i simply need just one of these and not the same duciplates, how do i avoid these?

(我只需要其中一个,而不是相同的duciplates,如何避免这些?)

here is my code so far, but i can't post the real api link as you need api keys, so i just placed a mock up placeholder.

(到目前为止,这是我的代码,但是由于您需要api键,因此我无法发布真正的api链接,因此我只是放置了一个模拟占位符。)

import time, requests, json


def test():

    x = 0

    while True:
        print(x)
        time.sleep(3)


        jsoninfo = [{'orderID': 'ed6ae4-ae36-ssebab5bba2beg', 'product': 'electos', 'cost': 200, 'Qty': 10}]

        if len(jsoninfo) != 0:

            if jsoninfo[0]['Qty'] > 5:

                print(jsoninfo)




        x+=1




test()
  ask by theLearner translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...