英文:
How to reorder two lists of different size based on one list?
问题
给定两个不同的列表,这两个列表共享一些相同的元素,但大小不同,如何根据第一个列表中元素的顺序重新排列第二个列表?例如:
对于:
a = ['a', 'b', 'e', 'z', 'f']
b = ['a', 'f', 'b', 'z', 'g', 'h']
如何将列表b重新排序为:
['a', 'b', 'z', 'f', 'g', 'h']
对于:
a = ['a', 'c', 'j', 'r', 'p']
b = ['b', 'c', 'a']
如何将列表b重新排序为:
['a', 'c', 'b']
到目前为止,我尝试过:
[x for y, x in sorted(zip(a, b))]
然而,我不明白如何控制这两个列表大小不同的情况。如何根据上述限制重新排列第二个列表?
英文:
Given two different lists which share elements in common and have different size, how to reorder the second list based on the order of the elements of the first one? For example:
For:
a = ['a', 'b', 'e', 'z', 'f']
b = ['a', 'f', 'b', 'z', 'g', 'h']
How to reorder list b into:
['a', 'b', 'z', 'f', 'g', 'h']
For:
a = ['a', 'c', 'j', 'r' , 'p']
b = ['b', 'c', 'a']
How to reorder list b into:
['a', 'c', 'b']
So far I tried to:
[x for y, x in sorted(zip(a, b))]
However, I dont understand how to control the fact that the lists have different size. How could I reorder the second list based on the above restrictions?
答案1
得分: 2
你可以提供一个键函数来根据索引进行排序。
a = ['a', 'b', 'e', 'z', 'f']
b = ['a', 'f', 'b', 'z', 'g', 'h']
from math import inf
res = sorted(b, key=lambda x: a.index(x) if x in a else inf)
print(res)
英文:
You can provide a key function to sort based on the index.
a = ['a', 'b', 'e', 'z', 'f']
b = ['a', 'f', 'b', 'z', 'g', 'h']
from math import inf
res = sorted(b, key=lambda x: a.index(x) if x in a else inf)
print(res)
答案2
得分: 2
你可以将列表a
的元素映射到数字顺序,然后使用Python的排序功能对数字进行排序。
a = ['a', 'b', 'e', 'z', 'f']
b = ['a', 'f', 'b', 'z', 'g', 'h']
order = {value: index for index, value in enumerate(a)}
solution = sorted(b, key=lambda x: order.get(x, len(b) + len(a)))
print(solution)
编辑:@jferard 更正了在列表b
中元素不在列表a
中时如何排序元素的情况。
英文:
you can map the list a
elements to numerical order and then use python sort functionality of sorting number
a = ['a', 'b', 'e', 'z', 'f']
b = ['a', 'f', 'b', 'z', 'g', 'h']
order = {value:index for index, value in enumerate(a)}
solution = sorted(b, key=lambda x: order.get(x, len(b)+len(a)))
print(solution)
edit: @jferard corrected the case critera where how to order element if element in list b not present in list a.
答案3
得分: 1
你可以创建一个基于a
的字典索引。对于在b
中重复的项目和在a
中不存在的项目,我们可以使用itertools.count
。
from itertools import count
a = ['a', 'b', 'e', 'z', 'f']
b = ['a', 'a', 'f', 'f', 'b', 'z', 'z', 'g', 'h', 'g', 'h', 'e']
dct_enum = {j: i for (i, j) in enumerate(a)}
idx_not_exist = count(len(a))
dct_index = {}
for i in b:
if i not in dct_index:
if i in dct_enum:
dct_index[i] = dct_enum[i]
else:
dct_index[i] = next(idx_not_exist)
sorted_b = sorted(b, key=dct_index.get)
# ['a', 'a', 'b', 'e', 'z', 'z', 'f', 'f', 'g', 'g', 'h', 'h']
# 对'b'进行排序后的字典索引:
# {'a': 0, 'b': 1, 'e': 2, 'z': 3, 'f': 4, 'g': 5, 'h': 6}
感谢@Kelly Bundy的贡献,还有一个更简短的方法,可以像下面这样写:
a = ['a', 'b', 'e', 'z', 'f']
b = ['a', 'a', 'f', 'f', 'b', 'z', 'z', 'g', 'h', 'g', 'h', 'e']
# Python >= 3.7
dct_index = {j: i for (i, j) in enumerate(dict.fromkeys(a + b))}
# 旧版本的Python
dct_index = {j: i for (i, j) in enumerate(a)}
for i in b:
if i not in dct_index:
dct_index[i] = len(dct_index)
sorted_b = sorted(b, key=dct_index.get)
# ['a', 'a', 'b', 'e', 'z', 'z', 'f', 'f', 'g', 'g', 'h', 'h']
# 经过排序后的字典索引:
# {'a': 0, 'b': 1, 'e': 2, 'z': 3, 'f': 4, 'g': 5, 'h': 6}
英文:
You can create dict
base index of a
. This works for repeated item in b
and for items do not exist in a
we can use itertools.count
.
from itertools import count
a = ['a', 'b', 'e', 'z', 'f']
b = ['a', 'a', 'f', 'f', 'b', 'z', 'z', 'g', 'h', 'g', 'h', 'e']
dct_enum = {j:i for (i, j) in enumerate(a)}
idx_not_exist = count(len(a))
dct_index = {}
for i in b:
if i not in dct_index:
if i in dct_enum:
dct_index[i] = dct_enum[i]
else:
dct_index[i] = next(idx_not_exist)
sorted(b, key=dct_index.get)
# ['a', 'a', 'b', 'e', 'z', 'z', 'f', 'f', 'g', 'g', 'h', 'h']
# sort 'b' base:
# {'a': 0, 'b': 1, 'e': 2, 'z': 3, 'f': 4, 'g':5, 'h':6}
By thanks @Kelly Bundy, For a shorter approach, we can write like the below:
a = ['a', 'b', 'e', 'z', 'f']
b = ['a', 'a', 'f', 'f', 'b', 'z', 'z', 'g', 'h', 'g', 'h', 'e']
# python >= 3.7
dct_index = {j:i for (i, j) in enumerate(dict.fromkeys(a+b))}
# Older python version
dct_index = {j:i for (i, j) in enumerate(a)}
for i in b:
if i not in dct_index:
dct_index[i] = len(dct_index)
# dct_index : {'a': 0, 'b': 1, 'e': 2, 'z': 3, 'f': 4, 'g': 5, 'h': 6}
print(sorted(b, key=dct_index.get))
答案4
得分: 1
a = ['a', 'c', 'j', 'r', 'p']
b = ['b', 'c', 'a']
c = []
for i in b:
if i in a:
c.insert(a.index(i), i)
for i in b:
if i not in a:
c.append(i)
print(c)
英文:
a = ['a', 'c', 'j', 'r' , 'p']
b = ['b', 'c', 'a']
c = []
for i in b:
if i in a:
c.insert(a.index(i), i)
for i in b:
if i not in a:
c.append(i)
print(c)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论