I'm trying to create two lists, where bracketed substrings in the second list will never occur in the first list.
Given a starting list of strings and an empty list:
word_list = ['{a==meliorate}>ed>','{a==meliorate}>s>','{a==meliorate}','{anew}','{annex}>ing>','{anvil}>ed>','{anvil}>ing>','{anvil}','<un<{ban}>ed>','<re<{write}']
new_list=[]
I would like to be able to split word_list, so that half of the words are popped to new_list. However, for a given bracketed {substring}, if it is found in word_list it should not be found in new_list and vice versa.
So we would have:
word_list = ['{anew}','{anvil}>ed>','{anvil}>ing>','<re<{apply}','<un<{ban}>ed>']
new_list=['{a==meliorate}>ed>','{a==meliorate}>s>','{a==meliorate}','<re<{write}','{annex}>ing>']
My attempt so far:
regex = re.compile('.*({[a-z]+}).*')
matches=[]
for element in word_list:
m = re.search(regex, element)
if m:
root = m.group(1)
matches.append(root)
while counter < len(word_list)/2:
randroot = random.choice(matches) #select a random {root}
indices = [i for i, e in enumerate(matches) if e == randroot] #get indices of all words with given root
for index in indices: #for each index of root-aligned words, appends corresponding word
new_list = word_list.pop(index)
However, my output seems to be random, with strings containing the bracketed elements appearing on both lists. Any help is much appreciated!