将列表中的字符串数据转换为 python 中的数值 |不可散列类型:'list'

String data in list to numeric values in python | unhashable type: 'list'

我有一个列表如下,它是一个巨大的列表,这只是其中的一部分。

 my_list= [['I. R. Palmer','U. Kersten'],
           ['H. Breitwieser', 'U. Kersten'],
           ['Halvard Skogsrud', 'Boualem Benatallah', 'Fabio Casati', 'Manh Q. Dinh'],
           ['Stefano Ceri', 'Piero Fraternali', 'Stefano Paraboschi']]

我想为列表中的每个字符串分配一个唯一的数值。如果字符串在其他地方重复,则为其分配相同的先前值

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

我试过了

pd.factorize(my_list)

但我得到

unhashable type: 'list'

pandas.factorize 对一维序列进行操作,但您有一个二维序列。而且由于您的 2D 序列不是规则形状(每个内部列表的长度不同),您无法通过重塑来解决这个问题。您看到的错误是因为 pandas 试图将内部列表视为类别而不是内部列表中的字符串。

您可以自己构建结果:

authors_map = {}  # I'm just guessing that they're authors
next_id = 0
new_list = []
for authors in my_list:
    new_authors = []
    for author in authors:
        if author not in authors_map:
            authors_map[author] = next_id
            next_id += 1
        new_authors.append(authors_map[author])
    new_list.append(new_authors)

您只能在 pd.factorize 中有一个一维序列。参考 doc

您可以使用np.concatenate将列表转换为一维

import numpy as np
print(np.concatenate(my_list))
# array(['I. R. Palmer', 'U. Kersten', 'H. Breitwieser', 'U. Kersten',
#   'Halvard Skogsrud', 'Boualem Benatallah', 'Fabio Casati',
#   'Manh Q. Dinh', 'Stefano Ceri', 'Piero Fraternali',
#   'Stefano Paraboschi'], dtype='<U18')

print(pd.factorize(np.concatenate(my_list)))

输出:

(array([0, 1, 2, 1, 3, 4, 5, 6, 7, 8, 9], dtype=int64),
 array(['I. R. Palmer', 'U. Kersten', 'H. Breitwieser', 'Halvard Skogsrud',
        'Boualem Benatallah', 'Fabio Casati', 'Manh Q. Dinh',
        'Stefano Ceri', 'Piero Fraternali', 'Stefano Paraboschi'],
       dtype=object))

您可以展平列表,使用 factorize 处理一维数组,通过 zip 创建字典并在嵌套列表理解中替换:

a = [y for x in my_list for y in x]
f1, f2 = pd.factorize(a)
d = dict(zip(f2[f1], f1))

new_list = [[d[y] for y in x] for x in my_list]
print (new_list)
[[0, 1], [2, 1], [3, 4, 5, 6], [7, 8, 9]] 

factorize + concatenate + cumsum + array_split

pd.factorize 通过散列运算。但是列表 中的值是 列表,它们不可散列。实际上,在任何情况下,您都不是在寻找哈希列表,而是在寻找单个值。

相反,您可以分解 展平的 列表并使用索引数组进行拆分:

import pandas as pd
import numpy as np

flattened = np.concatenate(my_list)
idx_split = np.array(list(map(len, my_list))).cumsum()[:-1]

res = [i.tolist() for i in np.array_split(pd.factorize(flattened)[0], idx_split)]

print(res)

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