Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sorting string lists and merging lists

7 views
Skip to first unread message

c.sc...@briefdomain.de

unread,
May 13, 2013, 6:35:03 AM5/13/13
to
Hi, I have 2 Lists like this:
list1 = ["ML2", "ML4", "ML6", "ML8", "ML10", "ML12"]
list2 = ["ML4", "ML6", "ML8", "ML10", "ML12", "ML14", "ML16", "ML18", "ML20"]

Now i want to merge these list and sort out double elements. At the moment i can do this, but i don't think it's that efficient:

new_list = list1

for element in list2:
if element not in list1:
new_list.append(element)

And after that I want to sort the lists that it is sorted after the number like:
ML2, ML4, ML6, etc.. (ML is always ML and won't be changed)

Is there a way to do this?

Peter Otten

unread,
May 13, 2013, 6:58:18 AM5/13/13
to pyth...@starship.python.net
c.sc...@briefdomain.de wrote:

> Hi, I have 2 Lists like this:
> list1 = ["ML2", "ML4", "ML6", "ML8", "ML10", "ML12"]
> list2 = ["ML4", "ML6", "ML8", "ML10", "ML12", "ML14", "ML16", "ML18",
> "ML20"]
>
> Now i want to merge these list and sort out double elements. At the moment
> i can do this, but i don't think it's that efficient:
>
> new_list = list1
>
> for element in list2:
> if element not in list1:
> new_list.append(element)

Put the items into a set:

>>> list1 = ["ML2", "ML4", "ML6", "ML8", "ML10", "ML12"]
>>> list2 = ["ML4", "ML6", "ML8", "ML10", "ML12", "ML14", "ML16", "ML18",
"ML20"]
>>> items = set(list1)
>>> items.update(list2)
>>> items
set(['ML14', 'ML16', 'ML10', 'ML12', 'ML18', 'ML20', 'ML2', 'ML6', 'ML4',
'ML8'])

> And after that I want to sort the lists that it is sorted after the number
> like: ML2, ML4, ML6, etc.. (ML is always ML and won't be changed)
>
> Is there a way to do this?

Define a key function that splits a string into numerical and non-numerical
parts:

>>> import re
>>> split = re.compile("(\d+)").split
>>> def key(s):
... parts = split(s)
... parts[1::2] = map(int, parts[1::2])
... return parts
...
['ml', 2, '']
>>> key("ml10")
['ml', 10, '']

Use the key to sort the set into a list:

>>> sorted(items, key=key)
['ML2', 'ML4', 'ML6', 'ML8', 'ML10', 'ML12', 'ML14', 'ML16', 'ML18', 'ML20']


Mike Müller

unread,
May 13, 2013, 6:51:37 AM5/13/13
to pyth...@python.org
Am 13.05.13 12:35, schrieb c.sc...@briefdomain.de:
Sets and sorting with `key` should solve your problem:

list1 = ["ML2", "ML4", "ML6", "ML8", "ML10", "ML12"]
list2 = ["ML4", "ML6", "ML8", "ML10", "ML12", "ML14", "ML16", "ML18", "ML20"]
merged = set(list1).union(set(list2))
sorted_by_number = sorted(merged, key=lambda x: int(x[2:]))
print sorted_by_number

Output:
['ML2', 'ML4', 'ML6', 'ML8', 'ML10', 'ML12', 'ML14', 'ML16', 'ML18', 'ML20']


HTH,
Mike

> _______________________________________________
> python-de maillist - pyth...@python.org
> http://mail.python.org/mailman/listinfo/python-de
>

Mike Müller

unread,
May 13, 2013, 7:17:06 AM5/13/13
to pyth...@python.org
Am 13.05.13 12:35, schrieb c.sc...@briefdomain.de:
Sets and sorting with `key` should solve your problem:

list1 = ["ML2", "ML4", "ML6", "ML8", "ML10", "ML12"]
list2 = ["ML4", "ML6", "ML8", "ML10", "ML12", "ML14", "ML16", "ML18", "ML20"]

c.sc...@briefdomain.de

unread,
May 13, 2013, 8:02:01 AM5/13/13
to
Thank you. I had something with lambda and key in my mind, but i didn't thought about it.
Still i'm doing the merging now with a distinct in SQL. (Django) With Q, I didn't read that distinct is available in Django ORM aswell, so sorry for that.

Btw. Thank you to all of you. never thought I will get an answer that fast.

Stefan Behnel

unread,
May 13, 2013, 8:14:15 AM5/13/13
to Die Deutsche Python Mailingliste
Mike Müller, 13.05.2013 13:17:
> merged = set(list1).union(set(list2))

Das Erzeugen des sets im hinteren Teil ist übrigens redundant. set.union()
kann mit beliebigen Iterables umgehen.

Stefan

0 new messages