简体   繁体   中英

how to filter list by specific string

I recently took a coding challenge for python. My python skills are not the greatest and ran into a question that I was not able to answer and was hoping someone would be able to finally give me the answer since it didn't show how to actually do it.

essentially--

you are given a multiline list of conversations(strings) such-

conversation = ['john 10:23:14 hey hows its going?',
                'sam 10:23:20 not much you?',
                'carl 10:23:14 hey guys']

how would you filter that conversation by the persons name- and output what was said by them, and another content filter to search for a specific word and output the whole line. ex. if you search for the word 'guys', it would output, 'carl 10:23:30 hey guys' or if you search the word 'hey', it would output both lines that have that word.

I think this should work

search = 'hey'
out = [m for m in conversation if search in m]

Of course you can put this as a function:

conversation = ['john 10:23:14 hey hows its going?',
                'sam 10:23:20 not much you?',
                'carl 10:23:14 hey guys']
def search_in(conv, keyword):
    return [m for m in conv if keyword in m]
print(search_in(conversation, 'hey'))

Getting all messages written by a specific person:

def get_messages_of(conv, name):
    return [m for m in conv if m.split()[0] == name]
print(get_messages_of(conversation, 'john'))

Gives you:

['john 10:23:14 hey hows its going?']

You can also filter by time eg

def get_messages_between_dates(conv, start_date, end_date):
    return [m for m in conv if start_date <= m.split()[1] <= end_date]
print(get_messages_between_dates(conversation, '10:23:10', '10:23:15'))

Output:

['john 10:23:14 hey hows its going?', 'carl 10:23:14 hey guys']

You can even get all Questions if you want to:

def isQuestion(message):
    start_words = ['who', 'what', 'when', 'where', 'why', 'how', 'is', 'can', 'does', 'do']
    text = ' '.join(message.split()[2:])
    return text.split()[0] in start_words or text.endswith('?')

def get_all_questions(conv):
    return [m for m in conv if isQuestion(m)]

print(get_all_questions(conversation))

This prints a list of all questions in the conversation

['john 10:23:14 hey hows its going?', 'sam 10:23:20 not much you?']

Iterate over the conversation and check if a given word is in each message:

word = 'hey'
for message in conversation:
    if word in message:
        print(message)

Change String value with your desired string

conversation = ['john 10:23:14 hey hows its going?',
                'sam 10:23:20 not much you?',
                'carl 10:23:14 hey guys']

string = 'hey'
for element in conversation:
    if string in element:
        print(element)

You can construct a new list containing only strings that have the value you want. With a for loop, that could look like this:

new_list = []
phrase = 'guys'
for sentence in conversation:
    if phrase in sentence:
        new_list.append(phrase)

A list comp would be better in this case in my opinion

phrase = 'guys'
new_list = [sentence for sentence in conversation if phrase in sentence]

Alternatively, you could use filter

new_list = [*filter(lambda sentence: phrase in sentence, conversation]

filter typically isn't used as much as the list comprehension I showed above, but it should work fine.

You can use List Comprehensions to provide an elegant way to create new lists. The following is the basic structure of a list comprehension:

newList = [ expression(element) for element in oldList if condition ]

conversation = ['john 10:23:14 hey hows its going?',
                'sam 10:23:20 not much you?',
                'carl 10:23:14 hey guys']

def get_sentence(keyword):
    return [ s for s in conversation if keyword in s]

and you can test the function: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM