I have a successful code,which adds the words to the paranthesis:but i need to remove the duplicates in it.
My code:
import re
import collections
class Group:
def __init__(self):
self.members = set()
self.text = []
with open('text1.txt') as f:
groups = collections.defaultdict(Group)
group_pattern = re.compile(r'^(\S+)\((.*)\)$')
current_group = None
for line in f:
line = line.strip()
m = group_pattern.match(line)
if m: # this is a group definition line
group_name, group_members = m.groups()
groups[group_name].members |= set(group_members.split(','))
current_group = group_name
else:
if (current_group is not None) and (len(line) > 0):
groups[current_group].text.append(line)
for group_name, group in groups.items():
print "%s(%s)" % (group_name, ','.join(set(group.members)))
print '\n'.join(group.text)
print
My text file:
Car(skoda,audi,benz,bmw)
The above mentioned cars are sedan type and gives long rides efficient
......
Car(audi,Rangerover,Hummer)
SUV cars are used for family time and spacious.
Ouputs as:
Car(skoda,benz,bmw,Rangerover,Hummer,audi)
The above mentioned cars are sedan type and gives long rides efficient
......
SUV cars are used for family time and spacious.
Expected output:
Car(skoda,audi,benz,bmw,Rangerover,Hummer)
The above mentioned cars are sedan type and gives long rides efficient
......
SUV cars are used for family time and spacious.
Here audi is the duplicate in the output i removed it, but its inserted at last instead of second position. Please help!Answers will be appreciated!