How To Find Duplicate Values In A List And Merge Them
So basically for example of you have a list like:
l = ['a','b','a','b','c','c']
The output should be:
So basically put together the values that are duplicated into a list,
l = ['a','b','a','b','c','c'] it=iter(sorted(l)) next(it) new_l= for i in sorted(l): new_l.append() if next(it,None)==i: new_l[-1].append(i) else: new_l.append()
But doesn't work, and if it does work it is not gonna be efficient
Sort the list then use
>>> from itertools import groupby >>> l = ['a','b','a','b','c','c'] >>> [list(g) for _, g in groupby(sorted(l))] [['a', 'a'], ['b', 'b'], ['c', 'c']]
EDIT: this is probably not the fastest approach, sorting is O(n log n) time complexity for the average case and not required for all solutions (see the comments)
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module