Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-检查字符串中是否有两个单词_Python - Fatal编程技术网

Python-检查字符串中是否有两个单词

Python-检查字符串中是否有两个单词,python,Python,我想检查Python中数组的每个元素中是否有两个单词“car”和“motorbike”。我知道如何在中用检查一个单词,但不知道如何处理两个单词。非常感谢您的帮助两个字的解决方案: for string in array: if 'car' in string and 'motorbike' in string.split(): print("Car and motorbike are in string") n字解决方案,用于检查test\u words中的所有字是否都

我想检查Python中数组的每个元素中是否有两个单词“car”和“motorbike”。我知道如何在中用
检查一个单词,但不知道如何处理两个单词。非常感谢您的帮助

两个字的解决方案:

for string in array:
    if 'car' in string and 'motorbike' in string.split():
        print("Car and motorbike are in string")
n字解决方案,用于检查
test\u words
中的所有字是否都在
字符串中:

test_words = ['car', 'motorbike']
contains_all = True

for string in array:
    for test_word in test_words:
        if test_word not in string.split()::
            contains_all = False
            break
    if not contains_all:
        break

if contains_all:
    print("All words in each string")
else:
    print("Not all words in each string")

使用辅助布尔值

car=False
 motorbike=False
 for elem in array:

        if "car" in elem:
            car=True
        if "motorbike" in elem:
            motorbike=True
        if car and motorbike:
            break

编辑:我刚读到“每个元素”。只需使用和。

我认为一个简单的解决方案是:

all(地图(lambda w:w文本,('car','motorbike'))
但这可能会有问题,这取决于您需要比较的挑剔程度:

>text='我们能在汽车店买摩托车吗?'
>>>全部(地图(文本中的lambda w:w,('car','motorbike'))
真的
“car”和“motorbike”这两个词不在
文本中
,这仍然是
True
。您可能需要在文字上完全匹配。我会这样做:

单词=(‘汽车’、‘摩托车’) >>>text='我们能在汽车店买摩托车吗?' >>>set(words).issubset(text.split()) 假的 >>>text=‘一辆汽车和一辆摩托车’ >>>set(words).issubset(text.split()) 真的

现在它工作了

我会使用
all
功能:

wanted_values = ("car", "motorbike")
all(vehicle in text for text in wanted_values)
如果我们有一个字符串列表:

l = ['some car and motorbike',
     'a motorbike by a car',
     'the car was followed by a motorbike']

lines_with_vehicles = [text for text in l
                       if all(vehicle in text for text in wanted_values)]
使用regex,您可以执行以下操作:

# no particular order
car_and_motorbike_pattern = re.compile(r'(car.*motorbike|motorbike.*car)')
all(car_and_motorbike_pattern.search(text) for text in list_of_expressions)

# This works too
car_or_motorbike_pattern = re.compile(r'(car|motorbike)')
get_vehicles = car_or_motorbike_pattern.findall
all(len(set(get_vehicles(text))) == 2 for text in list_of_expressions)
为我工作: 它提供了3种解决方案。两种方法使用列表理解,第三种方法使用map+lambda函数


我认为没有一种简单的方法可以做到这一点。您需要使用一个丑陋的逻辑,如下一个:

image_file_name = 'man_in_car.jpg'
if 'car' in image_file_name and 'man' in image_file_name:
    print('"car" and "man" were found in the image_file_name')
这对两个单词有效,但是如果您需要检查许多单词,那么最好使用上面链接中的代码


我希望能够做到以下几点:

if 'car' and 'man' in image_file_name:
    print('"car" and "man" were found in the image_file_name')
或:


但是这最后两段代码在python中还不起作用。

使用逻辑AND操作符:
如果cond1和cond2:
只需使用
操作符
全部(map(lambda w:w在文本中,('car','motorbike'))
就更干净了……从某种意义上说,你是对的。然而,这取决于你所说的清洁是什么意思。当你仔细阅读我的每一行代码时,很明显发生了什么,而在你的代码行中,我可能理解发生了什么,你也是,但是初学者可能不理解。我的回答是以一种初学者应该能够理解正在发生的事情的方式写的,而不是像一行纸那样被复制粘贴。然而,您的解决方案对同一事物占用的空间更少,甚至更快,您是对的
如果字符串中的'car'和字符串中的'motorbike'。split():
为什么不在这两种情况下进行拆分?使用
打印
指示参数到
issubset
的转换中存在匹配或不需要
设置
转换,该方法采用一个iterable:
设置(words).issubset(text.split())
if any(['car','man'] in image_file_name):
    print('"car" and "man" were found in the image_file_name')