在python中减去列表和重复项

时间:2018-01-14 10:57:32

标签: python list-comprehension

我有两个清单:

l1=['TUDCAPL.CLM_NUM_CD',
    'TUDCAPL.CRT_TS',
    'TUDCAPL.CLM_NUM_CD',
    'TUDCAPL.CRT_TS',
    'TUDCAPJ.CLM_NUM_CD',
    'TUDCAPJ.CRT_TS']

l2 = ['TUDCAPL.CLM_NUM_CD',
      'TUDCAPL.CRT_TS']

我希望我的结果是

l3 = ['TUDCAPL.CLM_NUM_CD',
      'TUDCAPL.CRT_TS',
      'TUDCAPJ.CLM_NUM_CD',
      'TUDCAPJ.CRT_TS']

我使用了l3 = [x for x in l1 if x not in l2] 但结果是['TUDCAPJ.CLM_NUM_CD','TUDCAPJ.CRT_TS'] 忽略了重复项。如何获取重复项以及其他唯一值?

4 个答案:

答案 0 :(得分:2)

复制列表(如果您不想更改org.json.JSONException: Value {"appid":466560,"newsitems":[{"gid":"2284879949551103234","title":"These are the top 100 Steam games of 2017","url":"http:\/\/store.steampowered.com\/news\/externalpost\/rps\/2284879949551103234","is_external_url":true,"author":"contact@rockpapershotgun.com (Alec Meer)","contents":"Another year over, a new one just begun, which means, impossibly, <em>even more games.<\/em> But what about last year? Which were the games that most people were buying and, more importantly, playing? As is now something of a tradition, Valve have let slip a big ol&#8217; breakdown of the most successful titl...","feedlabel":"Rock, Paper, Shotgun","date":1514919601,"feedname":"rps","feed_type":0,"appid":550},{"gid":"2284879949546841782","title":"ShiroGames - Bye bye 2017 and WELCOME 2018 !!!","url":"http:\/\/store.steampowered.com\/news\/externalpost\/steam_community_announcements\/2284879949546841782","is_external_url":true,"author":"Lord_brioche","contents":"Hello Northgardians, As the end of 2017 is upon us, we wanted to share with you a few thoughts. What a year! At the beginning of 2017, Northgard was about to start its 6-months Early Access period. At the time, the game was supposed to be a fun, solo strategy game with 4 clans and ShiroGames was onl...","feedlabel":"Community Announcements","date":1514818282,"feedname":"steam_community_announcements","feed_type":1,"appid":466560},{"gid":"2284879949521363459","title":"Best PC games of 2017","url":"http:\/\/store.steampowered.com\/news\/externalpost\/rps\/2284879949521363459","is_external_url":true,"author":"contact@rockpapershotgun.com (RPS)","contents":"The calendar&#8217;s doors have been opened and the games inside have been eaten. But fear not, latecomer &#8211; we&#8217;ve reconstructed the list in this single post for easy re-consumption. Click on to discover the best games of 2017. <a href=\"https:\/\/www.rockpapershotgun.com\/2017\/12\/25\/best-pc-games-of-2017\/#more-503951\" class=\"more-link\">(more&hellip;)<\/a> ","feedlabel":"Rock, Paper, Shotgun","date":1514206814,"feedname":"rps","feed_type":0,"appid":240720}],"count":57} at appnews of type org.json.JSONObject cannot be converted to JSONArray )。在<{1}}中的项目上循环,然后将其从新列表中删除。 l1函数将丢弃每个项目的第一次出现,而其他项目和订单不受影响。

l2

结果:

remove

答案 1 :(得分:1)

您可以使用set

l3 = set(l1)
l3.update(l2)
print l3

答案 2 :(得分:0)

我知道它不是最好的解决方案,但它会保留您的清单中的顺序和独特元素:

from collections import OrderedDict

l1=['TUDCAPL.CLM_NUM_CD',
    'TUDCAPL.CRT_TS',
    'TUDCAPL.CLM_NUM_CD',
    'TUDCAPL.CRT_TS',
    'TUDCAPJ.CLM_NUM_CD',
    'TUDCAPJ.CRT_TS']

l2 = ['TUDCAPL.CLM_NUM_CD',
      'TUDCAPL.CRT_TS']


d = OrderedDict(zip(l1,[None]*len(l1)))
d2 = OrderedDict(zip(l2,[None]*len(l2)))

d.update(d2)
print(d.keys()) # this will contain the resulting list of strings

答案 3 :(得分:-1)

尝试将列表复制到一组(这样您就可以获得唯一值):

l1 = ['TUDCAPL.CLM_NUM_CD', 
      'TUDCAPL.CRT_TS',
      'TUDCAPL.CLM_NUM_CD', 
      'TUDCAPL.CRT_TS', 
      'TUDCAPJ.CLM_NUM_CD', 
      'TUDCAPJ.CRT_TS']
s1 = set(l1)
print(s1)