【问题标题】:Replace unique values from nested list with numbers in python用python中的数字替换嵌套列表中的唯一值
【发布时间】:2018-02-19 23:01:10
【问题描述】:

如何用数字替换嵌套列表中的唯一值?

sample = [["P1","P13","P2","P2"],
          ["P2","P13P14","P1","P0","P1"],
          ["P1","P0","P3"],
          ["P17","P3","P15P15"],
          ["P1","P5"]]

从示例中我可以创建一个唯一值列表:

unique_sample = sorted(list(set(x for l in sample for x in l)))

期望的输出是为样本嵌套列表中的每个值从 unique_sample 返回索引

output = [[4,0,5,5],
          [5,7,4,0,4],
          [4,3,6],
          [8,6,2],
          [4,1]]

【问题讨论】:

  • set 是无序的,因此每次运行代码时索引都会发生变化。
  • 谢谢我在列表前面添加了排序
  • @ThomasJohnson,实际数字是否重要,例如第一行输出可以是[[1, 6, 3, 3],...吗?
  • 我认为你下面的答案正是我现在正在寻找的只是试图测试它

标签: python list nested unique


【解决方案1】:

这是一种方法:

from itertools import chain

sample = [["P1","P13","P2","P2"],
          ["P2","P13P14","P1","P0","P1"],
          ["P1","P0","P3"],
          ["P17","P3","P15P15"],
          ["P1","P5"]]

d = {j: i for i, j in enumerate(sorted(set(chain(*sample))))}

result = [list(map(d.get, i)) for i in sample]

# [[1, 2, 6, 6],
#  [6, 3, 1, 0, 1],
#  [1, 0, 7],
#  [5, 7, 4],
#  [1, 8]]

【讨论】:

  • 这就是我假设 OP 的意思,但请注意,它与他们的示例输出不匹配。
【解决方案2】:

你可以使用熊猫:

import pandas as pd
s  = pd.Series(sum(sample,[])).drop_duplicates().reset_index(drop=True)
unique_sample = pd.Series(s.index,index=s.values).to_dict()
[list(map(unique_sample.get, e)) for e in sample]
Out[48]: [[0, 1, 2, 2], [2, 3, 0, 4, 0], [0, 4, 5], [6, 5, 7], [0, 8]]

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 2016-04-03
    • 2019-09-06
    • 2015-04-26
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    相关资源
    最近更新 更多