Sort List Based on Another List

Python 3.x -- sort one list based on another list and then return both lists sorted

Is it possible to merge the last two lines into one line?

Yes it is. ordered_candidates excludes the points from the result, since you only choose the candidates with [x for _, x in...]. This basically only selects the second item from each tuple. Additionally, ordered_points sorts only the points. They both also sort in reverse with reverse=True.

Seems like you can just modify ordered_candidates to include both items in the (point, candidate) pairs.

>>> points = [4,7,3,2,7]
>>> candidates = ['a', 'b', 'c', 'd', 'e']
>>> sorted(zip(points, candidates), reverse=True)
[(7, 'e'), (7, 'b'), (4, 'a'), (3, 'c'), (2, 'd')]

Kotlin: Sort list based on another list (different objects)

The easiest approach would be to first create a mapping of the list you want to sort by. It should associate the id to the actual object.

Then you iterate the list you want to sort by and map it's values to the object you retrieved from the mapping you just created.

Here a minimal example:

// both can have more properties of course
data class Foo(val id: Int)
data class Bar(val id: Int)

val fooList = listOf(Foo(4), Foo(2), Foo(1), Foo(3))
val barList = listOf(Bar(1), Bar(2), Bar(3), Bar(4))

val idValueMap = barList.associateBy { it.id }

val sortedByOtherList = fooList.map { idValueMap[it.id] }

println(sortedByOtherList)

Result:

[Bar(id=4), Bar(id=2), Bar(id=1), Bar(id=3)]

How to sort one list based on another?

I think this answers your question:

>>> [x for x in Ref if x in Input]
>>> [3, 2, 11, 10, 9, 8, 5, 4]

Hope it helps.

UPDATE:
Making Input a set for faster access:

>>> Input_Set = set(Input)
>>> [x for x in Ref if x in Input_Set]
[3, 2, 11, 10, 9, 8, 5, 4]

How to sort a list by another list

This isn't the most performant way, but it's probably one of the simplest. Use a sorting method that sorts based on the object's field's location in the other array:

final sortedExamples = List.from(examples)
..sort((a, b) => ids.indexOf(a.id) - ids.indexOf(b.id));

This way is slightly more involved but more performant as you only need to iterate over each list once each. It involves making a map out of your list and then using that as the source of truth to rebuild a sorted list:

final ref = Map.fromIterable(examples, key: (e) => e.id, value: (e) => e);
final sortedExamples = List.from(ids.map((id) => ref[id]));

The first option is better if space is an issue, the second is better if speed is an issue.

Sorting list based on another list's order

An efficient solution is to first create the mapping from the ID in the ids (your desired IDs order) to the index in that list:

val orderById = ids.withIndex().associate { it.value to it.index }

And then sort your list of people by the order of their id in this mapping:

val sortedPeople = people.sortedBy { orderById[it.id] }

Note: if a person has an ID that is not present in the ids, they will be placed first in the list. To place them last, you can use a nullsLast comparator:

val sortedPeople = people.sortedWith(compareBy(nullsLast<String>) { orderById[it.id] })

Sort a list according to another list, with uneven length

Here's a fast solution. First build a dict mapping values to indices:

d = {v:i for i, v in enumerate(l1)}

Then use it to obtain sort keys:

r = sorted(l2, key=lambda v: d[v])

Creating d is O(len(l1)), and the sort is O(len(l2)*log(len(l2))).

Sort list of tuples based on another list

You can use sorted with an appropriate key function:

l1 = [(1, 'a', 'x'), (2, 'b', 'y'), (3, 'a', 'z'), (4, 'c', 'xyz')]
l2 = ['a', 'b', 'c']
d2 = {v: i for i, v in enumerate(l2)} # map elements to indexes
# {'a': 0, 'b': 1, 'c': 2}

sorted(l1, key=lambda x: d2[x[1]])
# [(1, 'a', 'x'), (3, 'a', 'z'), (2, 'b', 'y'), (4, 'c', 'xyz')]

Sorting list based on order of substrings in another list

key = {next((s for s in list_one if v in s), None): i for i, v in enumerate(list_two)}
print(sorted(list_one, key=key.get))

This outputs:

['a78', '67b', 'c11']


Related Topics



Leave a reply



Submit