如何根据一个列表对两个不同大小的列表进行重新排序?

huangapple go评论99阅读模式
英文:

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:

  1. a = ['a', 'b', 'e', 'z', 'f']
  2. b = ['a', 'f', 'b', 'z', 'g', 'h']

How to reorder list b into:

  1. ['a', 'b', 'z', 'f', 'g', 'h']

For:

  1. a = ['a', 'c', 'j', 'r' , 'p']
  2. b = ['b', 'c', 'a']

How to reorder list b into:

  1. ['a', 'c', 'b']

So far I tried to:

  1. [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

你可以提供一个键函数来根据索引进行排序。

  1. a = ['a', 'b', 'e', 'z', 'f']
  2. b = ['a', 'f', 'b', 'z', 'g', 'h']
  3. from math import inf
  4. res = sorted(b, key=lambda x: a.index(x) if x in a else inf)
  5. print(res)
英文:

You can provide a key function to sort based on the index.

  1. a = ['a', 'b', 'e', 'z', 'f']
  2. b = ['a', 'f', 'b', 'z', 'g', 'h']
  3. from math import inf
  4. res = sorted(b, key=lambda x: a.index(x) if x in a else inf)
  5. print(res)

答案2

得分: 2

你可以将列表a的元素映射到数字顺序,然后使用Python的排序功能对数字进行排序。

  1. a = ['a', 'b', 'e', 'z', 'f']
  2. b = ['a', 'f', 'b', 'z', 'g', 'h']
  3. order = {value: index for index, value in enumerate(a)}
  4. solution = sorted(b, key=lambda x: order.get(x, len(b) + len(a)))
  5. print(solution)

编辑:@jferard 更正了在列表b中元素不在列表a中时如何排序元素的情况。

英文:

you can map the list a elements to numerical order and then use python sort functionality of sorting number

  1. a = ['a', 'b', 'e', 'z', 'f']
  2. b = ['a', 'f', 'b', 'z', 'g', 'h']
  3. order = {value:index for index, value in enumerate(a)}
  4. solution = sorted(b, key=lambda x: order.get(x, len(b)+len(a)))
  5. 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

  1. from itertools import count
  2. a = ['a', 'b', 'e', 'z', 'f']
  3. b = ['a', 'a', 'f', 'f', 'b', 'z', 'z', 'g', 'h', 'g', 'h', 'e']
  4. dct_enum = {j: i for (i, j) in enumerate(a)}
  5. idx_not_exist = count(len(a))
  6. dct_index = {}
  7. for i in b:
  8. if i not in dct_index:
  9. if i in dct_enum:
  10. dct_index[i] = dct_enum[i]
  11. else:
  12. dct_index[i] = next(idx_not_exist)
  13. sorted_b = sorted(b, key=dct_index.get)
  14. # ['a', 'a', 'b', 'e', 'z', 'z', 'f', 'f', 'g', 'g', 'h', 'h']
  15. # 对'b'进行排序后的字典索引:
  16. # {'a': 0, 'b': 1, 'e': 2, 'z': 3, 'f': 4, 'g': 5, 'h': 6}

感谢@Kelly Bundy的贡献,还有一个更简短的方法,可以像下面这样写:

  1. a = ['a', 'b', 'e', 'z', 'f']
  2. b = ['a', 'a', 'f', 'f', 'b', 'z', 'z', 'g', 'h', 'g', 'h', 'e']
  3. # Python >= 3.7
  4. dct_index = {j: i for (i, j) in enumerate(dict.fromkeys(a + b))}
  5. # 旧版本的Python
  6. dct_index = {j: i for (i, j) in enumerate(a)}
  7. for i in b:
  8. if i not in dct_index:
  9. dct_index[i] = len(dct_index)
  10. sorted_b = sorted(b, key=dct_index.get)
  11. # ['a', 'a', 'b', 'e', 'z', 'z', 'f', 'f', 'g', 'g', 'h', 'h']
  12. # 经过排序后的字典索引:
  13. # {'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.

  1. from itertools import count
  2. a = ['a', 'b', 'e', 'z', 'f']
  3. b = ['a', 'a', 'f', 'f', 'b', 'z', 'z', 'g', 'h', 'g', 'h', 'e']
  4. dct_enum = {j:i for (i, j) in enumerate(a)}
  5. idx_not_exist = count(len(a))
  6. dct_index = {}
  7. for i in b:
  8. if i not in dct_index:
  9. if i in dct_enum:
  10. dct_index[i] = dct_enum[i]
  11. else:
  12. dct_index[i] = next(idx_not_exist)
  13. sorted(b, key=dct_index.get)
  14. # ['a', 'a', 'b', 'e', 'z', 'z', 'f', 'f', 'g', 'g', 'h', 'h']
  15. # sort 'b' base:
  16. # {'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:

  1. a = ['a', 'b', 'e', 'z', 'f']
  2. b = ['a', 'a', 'f', 'f', 'b', 'z', 'z', 'g', 'h', 'g', 'h', 'e']
  3. # python >= 3.7
  4. dct_index = {j:i for (i, j) in enumerate(dict.fromkeys(a+b))}
  5. # Older python version
  6. dct_index = {j:i for (i, j) in enumerate(a)}
  7. for i in b:
  8. if i not in dct_index:
  9. dct_index[i] = len(dct_index)
  10. # dct_index : {'a': 0, 'b': 1, 'e': 2, 'z': 3, 'f': 4, 'g': 5, 'h': 6}
  11. print(sorted(b, key=dct_index.get))

答案4

得分: 1

  1. a = ['a', 'c', 'j', 'r', 'p']
  2. b = ['b', 'c', 'a']
  3. c = []
  4. for i in b:
  5. if i in a:
  6. c.insert(a.index(i), i)
  7. for i in b:
  8. if i not in a:
  9. c.append(i)
  10. print(c)
英文:
  1. a = ['a', 'c', 'j', 'r' , 'p']
  2. b = ['b', 'c', 'a']
  3. c = []
  4. for i in b:
  5. if i in a:
  6. c.insert(a.index(i), i)
  7. for i in b:
  8. if i not in a:
  9. c.append(i)
  10. print(c)

huangapple
  • 本文由 发表于 2023年2月10日 04:20:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定