用python构建词汇表

2024-04-24 16:23:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个这样的字符串列表:

['1---d--e--g--gh','1---c---e--gh--', '1---ghj--h--h--', '1---g--gkk--h--', '1---d--dfe---fg', '1---c--d--dh--j', '1---f--gh--h--h', '1---fg-hg-hh-fg', '1---d--cd7--d--', '1---gghG--g77--', '1---hkj--kl--l-', '1---gged--ghjg-', '1---kk--k--k---', '1---gjklk--khgl', '1---c---d---dh-', '1---g---ghkk--k', '1---fH---h--g--', '1---f--gij---hj', '1---g--ghg---g-', '1---c---dc--cf-', '1---d---e--gh--', '1---l--lmnmlk-l', '1---d77---c--d-', '1---kj--k--lk-l', '1---g---gd--e--', '1---hhgh--d---h', '1---f--f---h---', '1---g--gkh-jkhg', '1---fg-hgh-fhfg', '1---k-k--klkj--', '1---g--l--kjhg-', 'gh--g---gh--g--', '1---f--df--fhij', '1---g--g--g---g', '1---g---gh-kh--', '1---g---gk--h--']

我想创建3种类型的词汇表:abc。你知道吗

a由至少一个短划线-b由至少两个--c由至少三个短划线---分隔。你知道吗

例如,1--d--d--dfd-dc---f---g--ghgf-ghg-hj--h应该给出:

a: {d, d, dfd, dc, f, g, ghgf, ghg, hj, h}
b: {d, d, dfd-dc, f, g, ghgf-ghg-hj, h}
c: {d--d--dfd-dc, f, g--ghgf-ghg-hj--h}

作为词汇表(我们跳过开头的1)。有人知道用python来实现这一点吗?你知道吗


Tags: 词汇表字符串列表dcghdhhjfg
3条回答

使用示例用例:

string = "1 d d dfd-dc -f -g ghgf-ghg-hj h"

def vocab_representation(string):
    import re
    letter_dict = {}
    # remove leading 1 and -, remove trailing -
    string = re.sub(r'^1?-*(.*\w).*$', r'\1', string)
    letter_dict['a'] = [x for x in string.split("-") if x]
    # No words with leading -
    letter_dict['b'] = [x for x in string.split(" ") if (x and x[0] != '-')]
    # No words with leading -
    letter_dict['c'] = c = [x for x in string.split(" -") if (x and x[0] != '-')]
    return letter_dict
res = vocab_representation(string)

输出:

{
 'a': ['d', 'd', 'dfd', 'dc', 'f', 'g', 'ghgf', 'ghg', 'hj', 'h'],
 'b': ['d', 'd', 'dfd-dc', 'ghgf-ghg-hj', 'h'],
 'c': ['d d dfd-dc', 'f', 'g ghgf-ghg-hj h']
}

使用更复杂的测试用例:

string = "gh g -gh g "
res = vocab_representation(string)

输出:

{
 'a': ['gh', 'g', 'gh', 'g'],
 'b': ['gh', 'g', 'g'],
 'c': ['gh g', 'gh g']
}
lines = ['1 d d dfd-dc -f -g ghgf-ghg-hj h']

a = []
b = []
c = []

def go():    
    for line in lines:
        line = line[1:]
        line = line.strip('-')
        global a, b, c
        a = line.split('-')
        b = line.split(' ')
        c = line.split(' -')

def sanitize():
    global a, b

    tmpa = []
    for s in a:
        if s != '':
            tmpa.append(s.strip('-'))

    tmpb = []
    for s in b:
        if s != '':
            tmpb.append(s.strip('-'))

    a = tmpa
    b = tmpb

go()
sanitize()
print("a: {" + ', '.join(a) + "}")
print("b: {" + ', '.join(b) + "}")
print("c: {" + ', '.join(c) + "}") 

可以对列表中的每个字符串使用列表理解:

string = '1 d d dfd-dc -f -g ghgf-ghg-hj h'
a = [i.strip("-") for i in string.split("-") if i and i.strip("-")!='1']
b = [i.strip("-") for i in string.split(" ") if i and i.strip("-")!='1']
c = [i.strip("-") for i in string.split(" -") if i and i.strip("-")!='1']

如果您有一个包含这些字符串的列表vps,您只需执行以下操作:

l =[[i.strip("-") for i in string.split("-") if i and i.strip("-")!='1'] for string in vps]

相关问题 更多 >