I wanted to do the action described in Compare two text of words, check and erase duplicates, and merge with Python using exactly the code:
# open files a.txt and b.txt and get the content as a list of lines
with open('a.txt') as f:
a = f.readlines()
with open('b.txt') as f:
b = f.readlines()
# get the string from the list
a_str = ''.join(a)
b_str = ''.join(b)
# get sets of unique words
a_set = set(a_str.split(" "))
b_set = set(b_str.split(" "))
# merge sets
c_set = a_set.union(b_set)
# write to a new file
with open('c.txt', 'w') as f:
f.write(' '.join(c_set))
It perfectly works according to the action of merging two texts erasing duplicates, but how can I create the new text c in the way that the words are reordered in columns and in alphabetic order?
To be more clear I have text one that is
text a:
NewYork London Paris
Rome Tokyo Berlin
Edinburgh LosAngeles Madrid
text b:
Madrid Cracow Porto
Rome Berlin Barcelona
Manchester Tokyo Dublin
I would like that the text c could be organized in 3 columns and with words in alphabetic order as:
Barcelona Berlin Cracow
Dublin Edinburgh London
LosAngeles Madrid Manchester
NewYork Paris Porto
Rome Tokyo