所以我看了一遍,我似乎无法找到我需要的东西,请注意我已经找到了如何删除其中一个副本但不能同时删除。这是我的字符串:
["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"]
这些是我的Discord Bot的文件,首先是所有文件,第二个是当前加载的文件。如果你还没弄清楚我想要做什么但我试图找到卸载的齿轮,所以我需要从列表中删除所有重复项。谢谢!
我正在使用discord.py 1.0.0a和Python 3.5.2
这是我目前的代码:
@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))
这是我不断得到的建议:
{'t', 'a', 'v', 'f', 'h', "'", 'k', 'd', 'c', 'i', 'l', 'p', '[', 'y', 'r', 'u', 'x', ' ', '+', ']', 'b', ',', 's', 'n', 'g', 'e', 'w', 'm', 'o'}
我真的无法在这里格式化,它让我发疯了哈哈 https://hastebin.com/irotuzafic.py
答案 0 :(得分:2)
假设您最初有两个列表:
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"))
然后找到卸载的那么简单:
all_cogs - loaded_cogs
答案 1 :(得分:0)
根据建议,set
很有用。
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
答案 2 :(得分:0)
要仅保留不在两个集合中出现的字词,您想要的操作是独占的,或者:
>>> a = set("abc")
>>> b = set("bcd")
>>> non_dupes = a ^ b
>>> print(non_dupes)
{'a', 'd'}