Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String_Python 3.x - Fatal编程技术网

Python在输出中查找整行

Python在输出中查找整行,python,string,python-3.x,Python,String,Python 3.x,我正在运行一个命令,其中输出是一个数字列表: output = subprocess.run(['command'], stdout = subprocess.PIPE) 输出(output.stdout.decode('utf-8'))如下所示: 1 534 89 4 57 9 我需要找出一个具体的数字是否不在列表中。问题是,如果我使用if num not in list:搜索num=3,我将得到true,因为数字534在该列表中 如何检查列表中是否有一个数字(在其自身的一行中)?只需拆分

我正在运行一个命令,其中输出是一个数字列表:

output = subprocess.run(['command'], stdout = subprocess.PIPE)
输出(
output.stdout.decode('utf-8')
)如下所示:

1
534
89
4
57
9
我需要找出一个具体的数字是否不在列表中。问题是,如果我使用
if num not in list:
搜索num=3,我将得到true,因为数字534在该列表中


如何检查列表中是否有一个数字(在其自身的一行中)?

只需拆分列表并检查“word”或“integer”,使用集合理解消除重复项:

if 3 in {int(x) for x in output.stdout.decode('utf-8').split()}:
通过直接输出
split
,也可以实现更简单的方法:

if "3" in output.stdout.decode('utf-8').split():

(如果整数可以从0开始,则功能较弱:
03

只需添加Jean Francois的答案即可。Split()默认情况下在空白处拆分,但由于您需要在行上拆分,我建议使用Split(“\n”)来进行拆分,这样代码将更有弹性

您也可以使用
re
模块:

import re

lookup=3

pattern = re.compile('\b{}\b'.format(lookup))

if pattern.search(output.stdout.decode('utf-8')):
    ...

num=3
更改为
num=“3”
可能重复的