In a list of tuples, return tuple[1] if tuple[0] is a duplicate of another tuple[0] in the list
Tag : python , By : George Handlin
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You almost never want to use index on a list. Keep track of where you are while iterating; don't try to find your position again from the value. In this case, what you really want is a "multidict", a dictionary that maps keys to collections of values. In this case, checksums to collections of names. Then, any checksum that maps to a set of more than 1 name, it's a dup, and the set is exactly the list of names you want to print out, so that's all it takes. >>> pairs = [("sumstring1","abc.txt"), ("sumstring2","def.txt"),
... ("sumstring1","ghi.txt"), ("sumstring2","jkl.txt")]
>>> dups = collections.defaultdict(set)
>>> for checksum, name in pairs:
... dups[checksum].add(name)
>>> dups
defaultdict(<class 'set'>, {'sumstring1': {'ghi.txt', 'abc.txt'}, 'sumstring2': {'def.txt', 'jkl.txt'}})
>>> dups = {checksum: names for checksum, names in dups.items() if len(names) > 1}
>>> dups
{'sumstring1': {'abc.txt', 'ghi.txt'}, 'sumstring2': {'def.txt', 'jkl.txt'}}
>>> dups = list(dups.values())
>>> dups = [tuple(names) for names in dups.values()]
>>> dups
[('ghi.txt', 'abc.txt'), ('def.txt', 'jkl.txt')]
|
Append tuple elements to a tuple of tuples in Python
Tag : python , By : user135518
Date : March 29 2020, 07:55 AM
Does that help If I have a tuple, e.g. x = (1, 2, 3) and I want to append each of its elements to the front of each tuple of a tuple of tuples, e.g. y = (('a', 'b'), ('c', 'd'), ('e', 'f')) so that the final result is z = ((1, 'a', 'b'), (2, 'c', 'd'), (3, 'e', 'f')), what is the easiest way? , Use zip and flatten the result: >>> x = (1, 2, 3)
>>> y = (('a', 'b'), ('c', 'd'), ('e', 'f'))
>>> tuple((a, b, c) for a, (b, c) in zip(x,y))
((1, 'a', 'b'), (2, 'c', 'd'), (3, 'e', 'f'))
>>> tuple((head, *tail) for head, tail in zip(x,y))
((1, 'a', 'b'), (2, 'c', 'd'), (3, 'e', 'f'))
|
Combining tuple elements from a list of tuples. Tuple elements are tuple and list
Tag : python , By : General Mills
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You can basically loop along, remembering what is the current key (initialized to None). key = None # This is 'A' or 'B' in your example, but it starts off as None
new_list = [] # This holds the final result
for r, l in [(r, l) for (l, r) in list_one]:
if r != key: # If the current is not the key, time to switch to a new one
id_ = l[0]
new_list.append({id_: []})
new_list[-1][id_].extend(l[1]) # Extend the current list
key = r # Make it the current key in any case.
>>> new_list
[{'id1': ['v1', 'v2', 'v3', 'v4', 'v5', 'v6']},
{'id3': ['v11', 'v12', 'v13', 'v14', 'v16']},
{'id6': ['v17', 'v18', 'v21']}]
|
Remove tuple from list of tuples if tuple's elements aren't in a list of strings
Tag : python , By : George Handlin
Date : March 29 2020, 07:55 AM
Does that help I'm working on some code where I need to remove a tuple from a list of tuples if the tuple doesn't contain all strings in a separate list. I've got it working in a for loop, but I'm trying to improve the efficiency of my code. As an example, if I have , You can use all with a nested comprehension: list_of_tups = [('R', 'S', 'T'), ('A', 'B'), ('L', 'N', 'E'), ('R', 'S', 'T', 'L'), ('R', 'S', 'T', 'L', 'N', 'E')]
needed_strings = ['R', 'S', 'T']
[t for t in list_of_tups if all(c in t for c in needed_strings)]
[('R', 'S', 'T'), ('R', 'S', 'T', 'L'), ('R', 'S', 'T', 'L', 'N', 'E')]
list_of_tups = [('R', 'S', 'T'), ('A', 'B'), ('L', 'N', 'E'), ('R', 'S', 'T', 'L'), ('R', 'S', 'T', 'L', 'N', 'E')]
needed_strings = set(['R', 'S', 'T'])
[t for t in list_of_tups if needed_strings.issubset(t)]
|
How to arrange a list of tuples so that the tuple associated with the highest value compared to other tuple is removed a
Date : March 29 2020, 07:55 AM
wish of those help you could use sort according to the length of the list, and use a dictionary so the last written key "wins". Then convert back to tuple or list or ... leave as dict: val = [(200, []), (300, [500, 200]), (400, [100, 200, 300]), (400, [])]
def largest_val_arrangement(val):
return tuple({k:v for k,v in sorted(val, key = lambda t : len(t[1]))}.items())
largest_val_arrangement(val)
((200, []), (400, [100, 200, 300]), (300, [500, 200]))
def largest_val_arrangement(val):
d = dict()
for k,v in val:
if k not in d or len(d[k]) < len(v):
d[k] = v
return tuple(d.items())
|