0

So I've looked all over and I can't seem to find what I need, note I have found how to remove ONE of the duplicates but not BOTH. Here is my string:

["animals", "api", "away", "bancheck", "botlists",
 "botstats", "bump", "core", "dblapi", "fun", "help",
 "im", "info", "information", "lists", "moderation",
 "modlog+", "promote", "recipe", "recycle bin",
 "registration", "revimage", "server", "shop", "social",
 "space", "spams", "spc", "speedtest", "streamalerts",
 "support", "sysinfo", "testing", "user", "utility",
 "watchfox", "welcome", "welcomer", "animals", "bancheck",
 "botlists", "botstats", "core", "fun", "help", "im",
 "info", "information", "moderation", "modlog+", "recipe",
 "revimage", "server", "shop", "social", "space", "spams",
 "spc", "speedtest", "support", "sysinfo", "testing", "user",
 "utility", "watchfox", "welcome", "welcomer"]

These are the files for my Discord Bot, first is all of them and the second is the ones that are currently loaded. If you haven't figured out what I'm trying to do yet I'm trying to find the unloaded cogs, so I need to remove all duplicates from the list. Thank you!

I am using discord.py 1.0.0a and Python 3.5.2

This is my current code:

    @commands.command()
    async def unloaded(self, ctx):
        cogs = sorted(os.listdir("/root/Python/Arctic-Fox/cogs"))
        a = '{}'.format(sorted(cogs)).replace('\'utils\', ', '')\
            .replace('\'__pycache__\', ', '').replace('.py', '')
        b = str(self.db).replace('cogs.', '')
        a1 = set(a)
        b1 = set(b)
        c = a1 | b1
        await ctx.send("```{}```".format(c))

This is what I keep getting with the suggestions:

{'t', 'a', 'v', 'f', 'h', "'", 'k', 'd', 'c', 'i', 'l', 'p', '[', 'y', 'r', 'u', 'x', ' ', '+', ']', 'b', ',', 's', 'n', 'g', 'e', 'w', 'm', 'o'}

I really can't format on here and it's making me mad lol https://hastebin.com/irotuzafic.py

furas
  • 134,197
  • 12
  • 106
  • 148
Kanin
  • 1
  • 1
  • Also this is my first time making a post, please don't be harsh if it is formatted really poorly. Thank you! – Kanin Nov 26 '17 at 01:07
  • Possible duplicate of [Remove all occurrences of a value from a list?](https://stackoverflow.com/questions/1157106/remove-all-occurrences-of-a-value-from-a-list) – hoefling Nov 26 '17 at 01:10
  • @hoefling This is not a duplicate of that, they are only removing one of the values and I wish to remove both. – Kanin Nov 26 '17 at 01:15
  • @Derek朕會功夫 I have tried to make it a set and I was given this: {'t', 'a', 'v', 'f', 'h', 'k', 'd', 'c', 'i', 'l', 'p', 'y', 'r', '[', 'u', 'x', ' ', '+', ']', 'b', '"', ',', 's', 'n', 'g', 'e', 'w', 'm', 'o'} – Kanin Nov 26 '17 at 01:16
  • @hoefling Oh I do apologize, it looks like I have misread that question multiple times. I will look into it. – Kanin Nov 26 '17 at 01:22
  • This is only a suggestion - maybe I am wrong! – hoefling Nov 26 '17 at 01:26
  • `a` and `b` keep all in one string, you have to split it into list of words before you use it with `set`. `set("hello")` will work like `set(["h", "e", "l", "l", "o"])` – furas Nov 26 '17 at 01:38
  • you have to add empty line before and after code to correctly format. Or use button `{}` – furas Nov 26 '17 at 01:38

3 Answers3

2

Assuming you have two lists at first:

all_cogs = set(("animals", "api", "away", "bancheck", "botlists",
 "botstats", "bump", "core", "dblapi", "fun", "help",
 "im", "info", "information", "lists", "moderation",
 "modlog+", "promote", "recipe", "recycle bin",
 "registration", "revimage", "server", "shop", "social",
 "space", "spams", "spc", "speedtest", "streamalerts",
 "support", "sysinfo", "testing", "user", "utility",
 "watchfox", "welcome", "welcomer"))

loaded_cogs = set(("animals", "bancheck",
 "botlists", "botstats", "core", "fun", "help", "im",
 "info", "information", "moderation", "modlog+", "recipe",
 "revimage", "server", "shop", "social", "space", "spams",
 "spc", "speedtest", "support", "sysinfo", "testing", "user",
 "utility", "watchfox", "welcome", "welcomer"))

Then finding the unloaded ones is as easy as:

all_cogs - loaded_cogs
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
0

As is suggested, set is useful.

a = set(['a', 'a', 'b', 'c']) # make a set without duplications
b = set(['a', 'd', 'e', 'e']) # make another set

c = a | b # now c contains all elements in a and b without duplications
d = a & b # d contains elements that exist both in a and b
dkato
  • 895
  • 10
  • 28
  • If you have only one word, it should be `a = set(['hello'])` in order to prevent to break the word into strings. – dkato Nov 26 '17 at 01:46
0

To keep only the words that don't appear in both sets, the operation you want is exclusive or:

>>> a = set("abc")
>>> b = set("bcd")
>>> non_dupes = a ^ b
>>> print(non_dupes)
{'a', 'd'}
alexis
  • 48,685
  • 16
  • 101
  • 161