简体   繁体   English

如何按特定字符串过滤列表

[英]how to filter list by specific string

I recently took a coding challenge for python.我最近接受了 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.我的 python 技能不是最好的,遇到了一个我无法回答的问题,希望有人能够最终给我答案,因为它没有显示如何实际操作。

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.您将如何按人名过滤该对话-和 output 他们所说的话,以及另一个内容过滤器来搜索特定单词和 output 整行。 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.如果你搜索“伙计们”这个词,它会是 output,“卡尔 10:23:30 嘿伙计们”,或者如果你搜索“嘿”这个词,它会 output 两行都有那个词。

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:当然你可以把它写成 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: 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:使用 for 循环,可能如下所示:

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或者,您可以使用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. filter通常不像我上面显示的列表理解那样使用,但它应该可以正常工作。

You can use List Comprehensions to provide an elegant way to create new lists.您可以使用 List Comprehensions 提供一种优雅的方式来创建新列表。 The following is the basic structure of a list comprehension:以下是列表推导的基本结构:

newList = [ expression(element) for element in oldList if condition ] newList = [表达式(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:您可以测试 function: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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