【问题标题】:Build vocabulary representations python [closed]构建词汇表表示python [关闭]
【发布时间】:2019-06-12 16:58:22
【问题描述】:

我有一个这种形式的字符串列表:

['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 中这样做的方法吗?

【问题讨论】:

    标签: python list dictionary vocabulary


    【解决方案1】:

    您可以对列表中的每个字符串使用列表推导:

    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]
    

    【讨论】:

    • 嗨,感谢您的回答。还有一个问题,如果我的名单名称是vps,我会简单地做a = [i.strip("-") for i in vps.items[1:].split("-") if i != ""]
    • 你希望它返回一个包含所有不同字符串列表的列表吗?
    • 我认为这未通过测试用例gh--g---gh--g--
    • 我希望每个词汇表都包含其类型的所有不同表示,所以是的,我想遍历列表中的所有字符串项并获得结果
    • 更新答案@joasa
    【解决方案2】:

    使用示例用例:

    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']
    }
    

    【讨论】:

      【解决方案3】:
      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) + "}") 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-23
        • 2014-02-08
        • 1970-01-01
        • 2019-12-28
        • 1970-01-01
        • 1970-01-01
        • 2013-06-13
        相关资源
        最近更新 更多