user47467
user47467

Reputation: 1093

Python reverting bigrams and trigrams

I have a list of bigrams and trigrams:

string = 'do not be sad'

a_list: = ['do', 'not', 'do not', 'be', 'not be', 'do not be', 'sad', 'be sad', 'not be sad']

I was wondering if there is a function to reverse the bigram and trigram in a_list? I know I could join all the strings and remove duplicates, but that loses the structure of the sentence. I'm looking if someone has any tips so that the a_list can be reverted back to its original string.

Desired output would be:

b_list = ['do not be sad']

Upvotes: 0

Views: 998

Answers (2)

zondo
zondo

Reputation: 20336

Use a list comprehension:

a_sentence = [" ".join(word for word in a_list if len(word.split()) == 1)]
print(a_sentence)

# Output: ['do not be sad']

Upvotes: 0

danidee
danidee

Reputation: 9624

Try this

string = 'do not be sad'
string = string.split()

a_list = ['do', 'not', 'do not', 'be', 'not be', 'do not be', 'sad', 'be sad', 'not be sad']

new = []

for a in string:
    for b in a_list:
        if a == b:
            new.append(b)

print([' '.join(new)])

Output

['do not be sad']

and we can make it into a nice one-liner

print([' '.join([b for a in string for b in a_list if a == b])])

EDIT: IN response to zondo's comment i decided to edit my answer, moreover i found this problem very interesting

a_list = ['do', 'not', 'do not', 'be', 'not be', 'do not be', 'sad', 'be sad', 'not be sad']
a_list = ['This', 'is', 'This is', 'my', 'is my', 'This is my', 'car', 'my car', 'is my car']
a_list = ['i', 'am', 'i am', 'a' , 'am a', 'i am a', 'boy', 'a boy', 'am a boy']

largest = max(a_list, key=len) # get the longest sub word in the list

# loop through and if all words of a sublist don't exist in the largest sub word then join them together
for elem in a_list:
    sp = elem.split()
    if all(i not in largest for i in sp):
        if a_list.index(elem) < a_list.index(largest):
            print([elem + ' ' + largest])
        else:
            print([largest + ' ' + elem])

i also created several test cases to test my solution, and they all passed

Upvotes: 1

Related Questions