删除重复的打印值(Python)

qnyhuwrf  于 2023-01-24  发布在  Python
关注(0)|答案(1)|浏览(108)

当我为NewsAPI编写代码时,我收到了将近800个值,在这800个值中,只有4个左右是非重复的,这取决于关键字。
是否有办法阻止重复生成?
下面是我的代码:

news_sources = newsapi.get_sources()

for source in news_sources['sources']:
    #print(source['name'])

    all_articles = newsapi.get_everything(
        q='shooting',
        language='en',
        #from_param='2023-01-22',
        #to='2023-01-22'
    )
    for article in all_articles['articles']:
        #print('Source : ', article['source']['name'])
        #print('Title : ', article['title'])
        #print('Date : ', article['publishedAt'])
        print('Url : ', article['url'], '\n\n')

我看到了这个问题以前的答案,但是不确定它是否适用于我的代码

var isDuplicated = false;
for (var new in news) {
   if (new.title == articleModel.title) {
      isDuplicated = true;
   }
}

if (!isDuplicated) {
   // Now you can add it
   news.add(articleModel);
}
b4lqfgs4

b4lqfgs41#

将其转换为集合并检索回列表

unique_sources = list(set([source['name'] for source in news_sources['sources']]))

相关问题