gpt4 book ai didi

python - 使用每个条目实例变量删除列表中重复项的最快方法

转载 作者:行者123 更新时间:2023-12-05 09:33:54 24 4
gpt4 key购买 nike

长话短说

有没有更快的办法

listOfClasses = [fooA, fooB, fooC]
setOfStrings = {c.string for c in listOfClasses}
newListOfClasses = []
for c in listOfClasses:
if c.string in setOfStrings:
newListOfClasses.append(c)
setOfStrings.remove(c.string)

限制/注意事项:

  • listOfClasses:

    • 条目不一定是唯一的,可能包含重复的类
    • 参赛作品数量通常在 3-4 人左右,但也可能多达 20 人
  • newListOfClasses:

    • 不能创建新类,它必须使用相同的对象(这些对象不是我的,我不知道它们是如何初始化的,所以我不知道它们里面除了字符串之外还有什么)。
    • 结果必须至少是可迭代的
    • 结果顺序无关紧要
    • 可以覆盖原来的listOfClasses

假设我有一个类

class Foo(object):
def __init__(self,string):
self.string = string

还有一个类列表,我想在其中删除所有具有重复“字符串”实例变量的类

fooA = Foo("alice")
fooB = Foo("alice")
fooC = Foo("His Royal Highness The Prince Philip, Duke of Edinburgh, Earl of Merioneth, Baron Greenwich, Royal Knight of the Most Noble Order of the Garter, Extra Knight of the Most Ancient and Most Noble Order of the Thistle, Member of the Order of Merit, Grand Master and First and Principal Knight Grand Cross of the Most Excellent Order of the British Empire, Knight of the Order of Australia, Additional Member of the Order of New Zealand, Extra Companion of the Queen’s Service Order, Royal Chief of the Order of Logohu, Extraordinary Companion of the Order of Canada, Extraordinary Commander of the Order of Military Merit, Lord of Her Majesty’s Most Honourable Privy Council, Privy Councillor of the Queen’s Privy Council for Canada, Personal Aide-de-Camp to Her Majesty, Lord High Admiral of the United Kingdom.")

listOfClasses = [fooA, fooB, fooC]

在这里,我想删除 fooAfooB(哪个无关紧要),这样我就只剩下

listOfClasses = [fooB, fooC] # for example

到目前为止,我有以下内容:

setOfStrings = {c.string for c in listOfClasses}
newListOfClasses = []
for c in listOfClasses:
if c.string in setOfStrings:
newListOfClasses.append(c)
setOfStrings.remove(c.string)

对于上述我得到以下时间:

# len(listOfClasses) = 3
2.22 ms ± 24.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
# len(listOfClasses) = 20
2.29 ms ± 119 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

最佳答案

在字典理解中使用字典的唯一键应该非常快:

list({cls.string: cls for cls in listOfClasses}.values())

完整示例:

class Foo(object):
def __init__(self, string):
self.string = string

fooA = Foo("alice")
fooB = Foo("alice")
fooC = Foo("His Royal Highness")

listOfClasses = [fooA, fooB, fooC]

print(list({cls.string: cls for cls in listOfClasses}.values()))

关于python - 使用每个条目实例变量删除列表中重复项的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66945230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com