繁体   English   中英

如何在Python中编码列表中的单词

[英]How to encode words in a list in Python

我有一本字典,其中每个词都作为键和对应的整数值,例如:

 {'me': 41, 'are': 21, 'the': 0}

我有一个数据框,其中有一列已被标记的单词列表,例如:

['I', 'liked', 'the', 'color', 'of', 'this', 'top']
['Just', 'grabbed', 'this', 'today', 'great', 'find']

如何将这些单词中的每个单词编码为字典中的相应值。 例如:

[56, 78, 5, 1197, 556, 991, 40] 

那怎么办

word2key = {'me': 41, 'are': 21, 'the': 0}
words = ['Just', 'grabbed', 'this', 'today', 'great', 'find']
default = 'unknown'
output = [word2key.get(x, default) for x in words]

如果要将'Just''just'映射到相同的值,则可能要使用x.lower()

使用字典和清单

以下使用字典( final_dictionary )确定单词的ID。 如果您有一个预设的ID字典,那就太好了。

def encode_tokens(tokens):
    encoded_tokens = tokens[:]
    for i, token in enumerate(tokens):
        if token in final_dictionary:
            encoded_tokens[i] = final_dictionary[token]
    return encoded_tokens

print(encode_tokens(tokens))

添加和维护ID

如果您正在动态分配ID,则可以实现一个类(请参见下面的内容)。 但是,如果您有一个预先定义的id字典,则可以传入关键字参数di

token_words_1 = ['I', 'liked', 'the', 'color', 'of', 'this', 'top']
token_words_2 = ['I', 'liked', 'to', 'test', 'repeat', 'words']

class AutoId:
    def __init__(self, **kwargs):
        self.di = kwargs.get("di", {})
        self.loc = 0
    def get(self, value):
        if value not in self.di:
            self.di[value] = self.loc
            self.loc += 1
        return self.di[value]
    def get_list(self, li):
        return [*map(self.get, li)]

encoding = AutoId()
print(encoding.get_list(token_words_1))
print(encoding.get_list(token_words_2))

假设您的字典位于名为d的变量中,并且列表名为l

d = {'me': 41, 'are': 21, 'the': 0}
l = ['I', 'liked', 'the', 'color', 'of', 'this', 'top']

print(l)
c = 0
while c < len(l):
    try:
        l[c] = d[l[c]]
    except:
        l[c] = None
    c += 1

print(l)
from itertools import chain
import numpy as np

# d = {'me': 41, 'are': 21, 'the': 0}
l1 = ['I', 'liked', 'the', 'color', 'of', 'this', 'top']
l2 = ['Just', 'grabbed', 'this', 'today', 'great', 'find']

# This is just for data generation for the sake of a complete example.
# Use your already given d here instead.
d = {k: np.random.randint(10) for k in chain(l1, l2)}
print(d)

l1_d = [d.get(k, 0) for k in l1]  # <- this is the actual command you need
print(l1_d)

l2_d = [d.get(k, 0) for k in l2]
print(l2_d)

结果:

{'I': 3, 'liked': 3, 'the': 8, 'color': 7, 'of': 3, 'this': 5,
 'top': 3, 'Just': 6, 'grabbed': 0, 'today': 0, 'great': 7, 'find': 0}
[3, 3, 8, 7, 3, 5, 3]
[6, 0, 5, 0, 7, 0]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM