简体   繁体   English

只从包含某些字符的列表中打印出字符串

[英]Only print out strings from a list that contain certain characters

Let's assume following list让我们假设以下列表

my_list = ['bottle', 'coffee', 'insufficient', 'differential', 'cat']

I want to write a function that takes two parameters as the input where m are the characters as a string that I want to find (eg 'ff') and n is the number of strings as an integer that I want to output.我想编写一个函数,它接受两个参数作为输入,其中 m 是作为我想要查找的字符串的字符(例如 'ff'),n 是作为我想要输出的整数的字符串数。

So def contains_strings('ff', 2):所以def contains_strings('ff', 2):

should output:应该输出:

['coffee', 'insufficient']

Both words contain 'ff' and the output is 2 in total.这两个词都包含 'ff',输出总共为 2。 The second parameter can be random.第二个参数可以是随机的。 So I don't care if the function outputs.所以我不在乎函数是否输出。 ['coffee', 'insufficient'] or ['differential', 'insufficient'] ['咖啡','不足'] 或 ['差异','不足']

I was only able to write a function that is able to output words that start with certain characters:我只能编写一个能够输出以某些字符开头的单词的函数:

import random as _random

my_list = ['bottle', 'coffee', 'insufficient', 'differential', 'cat']

def starting_letters(m: str, n: int):
    a = list(filter(lambda x: x.lower().startswith(m), my_list)) 
    print(_random.choices(a, k=n)) 
  

starting_letters('c', 2)

Output:输出:

['coffee', 'cat']

Maybe like this:也许是这样的:

import random as _random

my_list = ['bottle', 'coffee', 'insufficient', 'differential', 'cat']

def contains_strings(m: str, n: int):
      a = list(filter(lambda x: m in x.lower(), my_list)) 
      return _random.sample(a, k=n)

print(contains_strings('ff', 2))

#['coffee', 'insufficient']

Here is how I would do it:这是我将如何做到的:

import random as _random

my_list = ['bottle', 'coffee', 'insufficient', 'differential', 'cat']

def starting_letters(m: str, n: int):
    a = list(filter(lambda x: m in x.lower(), my_list)) 
    print(_random.sample(a, n))

Notice how I replaced the random.choices() method with the random.sample() method.注意我是如何用random.choices()方法替换random.sample()方法的。 The reason is because with the random.choices() method, you may get the element from the same index multiple times in the output, whereas the random.sample() method ensures that every returned element is from a unique index of the list.原因是因为使用random.choices()方法,您可能会在输出中多次从同一索引中获取元素,而random.sample()方法确保每个返回的元素都来自列表的唯一索引。

You can use regex here to match patterns in strings您可以在此处使用regex来匹配字符串中的模式

import re
import random as _random

my_list = ['bottle', 'coffee', 'insufficient', 'differential', 'cat']


def contains_pattern(pattern: str, n: int):
    regex = re.compile(f'{pattern}')
    a = [ans for ans in my_list if regex.search(ans)]
    return _random.choices(a, k=n)

print(contains_pattern("ff",2))

# ['insufficient', 'coffee']

import random as _random将随机导入为 _random

my_list = ['bottle', 'coffee', 'insufficient', 'differential', 'cat'] my_list = ['瓶子','咖啡','不足','差异','猫']

def starting_letters(m: str, n: int): a = list(filter(lambda x: m in x, my_list)) print(_random.choices(a, k=n)) def starting_letters(m: str, n: int): a = list(filter(lambda x: m in x, my_list)) print(_random.choices(a, k=n))

starting_letters('ff',2)起始字母('ff',2)

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

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