list approach due nested order - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: list approach due nested order (/thread-21608.html) |
list approach due nested order - 3Pinter - Oct-07-2019 Horrible thread name, but let me try and explain it. I put lists (with 2 (always) values) in a main-list and I want to cleanup the duplicates in that main-list. The order of the 2 values don't matter to me and are therefor the same. mainlist = [] listA = ["a", "b"] listB = ["b", "a"] listC = [1,2] mainlist = [ listA, listB ] unique-mainlist = [ ["a", "b"], [1,2] ] RE: list approach due nested order - stranac - Oct-07-2019 Since the order doesn't matter, you might want to consider using a set of frozensets: >>> unique = set() >>> unique.add(frozenset(['a', 'b'])) >>> unique.add(frozenset(['b', 'a'])) >>> unique.add(frozenset([1, 2])) >>> unique {frozenset({'a', 'b'}), frozenset({1, 2})} RE: list approach due nested order - 3Pinter - Oct-07-2019 frozenset... awesome. New functionality for me, but your suggestion certainly works Thanks!. Tiny side question: from a pythonic point of view is it better to - check if a list (or reversed) exists in main-list - if so, skip, else add Or like my current approach: - dump all in main-list - frozenset them Thinking in large numbers here. RE: list approach due nested order - Gribouillis - Oct-07-2019 Combining frozenset with unique_everseen yields my preferred solutionfrom more_itertools import unique_everseen def create(): yield ['a', 'b'] yield ['b', 'a'] yield [1, 2] unique = list(unique_everseen(create(), key=frozenset)) print(unique)
RE: list approach due nested order - 3Pinter - Oct-07-2019 Hmmm, more_itertools isn't installed. Unsure if I can install this. RE: list approach due nested order - Gribouillis - Oct-07-2019 3Pinter Wrote:Unsure if I can install this.Try
RE: list approach due nested order - 3Pinter - Oct-07-2019 # mainlist is a massive list joinlist = [] unique = set() for m in mainlist: unique.add(frozenset(m)) for u in unique: joinlist.append(list(u)) if joinlist: for join_pair in joinlist: #do something joinelements(join_pair)This is working, but feels a bit clumpsy. And looking at the yield functionality ... I think my coding could be improved? |