0

Delete a word next to another one(python)

i need to do the capslock part of this code list1["red","one"] import random #input input1=input("min 3 words").split() print(" ") if ("dei")in input1: input1.remove(#REMOVE THE WORD NEXT TO DEI) text=" ".join(input1) print(text, random.choice(list1))

1st Feb 2017, 11:09 AM
Guido Parlatore
Guido Parlatore - avatar
2 odpowiedzi
+ 1
if i get your request right you want this: if "dei"in input1: input1.remove(input1[input1.index("dei")+1]) text=" ".join(input1) print(text, random.choice(list1)) with the list.index() function you call the index of 'dei' and then you can go one further by adding 1 to the index. Plugging that index to the list to get the word which than can be removed.
1st Feb 2017, 12:30 PM
Marvin Taschenberger
Marvin Taschenberger - avatar
0
def removeNext(str1, str2): rtn = "" skips = 0 toSkip = False for word in str1.split(): if(toSkip): toSkip = False continue if(word == str2): if(skips == 0): toSkip = True skips = 1 rtn = rtn + word + " " else: rtn = rtn + word + " " return rtn[:-1] a = "de dei di" if("dei" in a.split()): input1 = removeNext(a, "dei")
1st Feb 2017, 12:46 PM
Thomas Sudeep Benardo