Exclude words with specific endings from list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Exclude words with specific endings from list (/thread-9392.html) |
Exclude words with specific endings from list - Epileptiker - Apr-05-2018 Hey, is there a way to exclude words from a list that have specific endings, like every single word that ends with -s or -en? In my script i import some lists from external files to my script and these lists are very long and i can't delete the specific words by myself, because it would take days. So i thought there may be a python-command or something. This is my script: import random, time, tweepy, ast with open('liste1.txt') as infile: list1 = ast.literal_eval(infile.read()) with open('liste1.txt') as infile: list1 = ast.literal_eval(infile.read()) with open('liste1.txt') as infile: list1 = ast.literal_eval(infile.read()) def one(): return random.choice(list1) + random.choice(list2) def two(): return random.choice(list2) + random.choice(list3) #I'm working on a Twitter bot, that's just some twitter stuff down here consumer_key = 'XXX' consumer_secret = 'XXX' access_token = 'XXX' access_token_secret = 'XXX' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) while True: postthis = random.choice([one,two])() if len(postthis) <= 140: api.update_status(status=postthis) time.sleep(10)Thanks :) RE: Exclude words with specific endings from list - nilamo - Apr-05-2018 First, why are you reading the file three times? That's just unnecessary. But sure, you can totally do that. Something as simple as: list1 = [item for item in list1 if not item.endswith("-s") and not item.endswith("-en")] RE: Exclude words with specific endings from list - Epileptiker - Apr-05-2018 (Apr-05-2018, 07:54 PM)nilamo Wrote:list1 = [item for item in list1 if not item.endswith("-s") and not item.endswith("-en")] Okay i just tried it by copying this code in my script right here, but when i run it, it still posts words with these two endings. No error message. What have i done wrong? ... with open('list2.txt') as infile: list2 = ast.literal_eval(infile.read()) list2 = [item for item in list2 if not item.endswith("-s") and not item.endswith("-en")] RE: Exclude words with specific endings from list - nilamo - Apr-05-2018 Do you have some example input/output? RE: Exclude words with specific endings from list - Epileptiker - Apr-06-2018 I replaced the content of my list #2 with the items ["aaa", "bbb", "ccc"] and excluded every word that ends with -b and -c. When i run the module, it still posts and alternates between all three items. This is the whole script right now. import random, time, tweepy, ast with open('list1.txt') as infile: list1 = ast.literal_eval(infile.read()) with open('list2.txt') as infile: list2 = ast.literal_eval(infile.read()) list2 = [item for item in list2 if not item.endswith("-b") and not item.endswith("-c")] with open('list3.txt') as infile: list3 = ast.literal_eval(infile.read()) def one(): return random.choice(list1) + random.choice(list2) def two(): return random.choice(list2) + random.choice(list3) consumer_key = 'XXX' consumer_secret = 'XXX' access_token = 'XXX' access_token_secret = 'XXX' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) while True: postthis = random.choice([one])() if len(postthis) <= 140: api.update_status(status=postthis) time.sleep(10) RE: Exclude words with specific endings from list - nilamo - Apr-06-2018 (Apr-05-2018, 08:11 PM)nilamo Wrote: Do you have some example input/output? |