0

I have a list of strings of this form:

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

I want to create vocabulary representations of 3 types : a, b, c.

a are separated by at least one dash -, b by at least two --, and c by at least three dashes ---.

For example, 1--d--d--dfd-dc---f---g--ghgf-ghg-hj--h should give:

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}

As vocabulary representations (we skip the 1 in the beginning). Does anyone know a way to do that in python?

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
joasa
  • 946
  • 4
  • 15
  • 35

3 Answers3

2

You can use list comprehension for each string in the list:

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

If you have a list vps containings those strings, you can just do:

l =[[i.strip("-") for i in string.split("-") if i and i.strip("-")!='1'] for string in vps]
Ricky Kim
  • 1,992
  • 1
  • 9
  • 18
DSC
  • 1,153
  • 7
  • 21
  • hi, thanks for your answer. one more question, if my list name is `vps`, i will simply do `a = [i.strip("-") for i in vps.items[1:].split("-") if i != ""]` ? – joasa Jun 12 '19 at 17:14
  • Do you want it to return a list containing all the different string lists? – DSC Jun 12 '19 at 17:17
  • I think this fails the test case `gh--g---gh--g--` – TomNash Jun 12 '19 at 17:18
  • I want each vocabulary to contain all different representations of its type, so yes I want to iterate over all string items in the list and get the results – joasa Jun 12 '19 at 17:21
  • updated answer @joasa – DSC Jun 12 '19 at 17:21
1

Using example use case:

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)

Output:

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

Using more complex test case:

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

Output:

{
 'a': ['gh', 'g', 'gh', 'g'],
 'b': ['gh', 'g', 'g'],
 'c': ['gh--g', 'gh--g']
}
TomNash
  • 3,147
  • 2
  • 21
  • 57
0
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) + "}")