Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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 如何使用lambda函数在列表/数组中查找具有匹配字母的字符串?_Python_Mongodb_Lambda - Fatal编程技术网

Python 如何使用lambda函数在列表/数组中查找具有匹配字母的字符串?

Python 如何使用lambda函数在列表/数组中查找具有匹配字母的字符串?,python,mongodb,lambda,Python,Mongodb,Lambda,我正在使用lambda函数、python3.6和mongodbatlas。在mongodb中,我有一个这样的收藏。集合名称配置文件。下面是收集结构 "_id" : ObjectId("5db234df92b0ce00016932f3") "username" : "testing" "channel" : [ "abc", "efg", "cde", "xyz" ] "about" : "this is a test case" 我们有多个与上面类似的行。现在我正在使用python,我编写la

我正在使用lambda函数、python3.6和mongodbatlas。在mongodb中,我有一个这样的收藏。集合名称配置文件。下面是收集结构

"_id" : ObjectId("5db234df92b0ce00016932f3")
"username" : "testing"
"channel" : [ "abc", "efg", "cde", "xyz" ]
"about" : "this is a test case"
我们有多个与上面类似的行。现在我正在使用python,我编写lambda函数来查找通道数组中匹配字母的字符串。查找下面的lambda函数

profile = db.profile
name = event['cname']

ch = list(profile.aggregate([{
    "$match" : { "username" : "testing" }
    }, 
    {
        "$project" : {
            "channel" : 1
            }
    }
    ]))

ch1 = json.loads(json.dumps(ch, default=json_util.default))
ch2 = [document["channel"] for document in ch1]
new_list = []
for i in ch2:
    if(re.findall(name, i)):
        new_list.append(i)  
return new_list
我已通过“cname”:“c”项。但是我得到了这样的错误

Response:
{
 "errorMessage": "expected string or bytes-like object",
 "errorType": "TypeError",
 "stackTrace": [
[
  "/var/task/lambda_function.py",
  51,
  "lambda_handler",
  "if(re.findall(search, i)):"
],
[
  "/var/lang/lib/python3.6/re.py",
  222,
  "findall",
  "return _compile(pattern, flags).findall(string)"
]
]
}
我也尝试了搜索,但我得到了同样的结果,我需要像下面这样的输出

Input: "cname" : "c"
output: "abc"
        "cde"
请您帮助我解决问题,谢谢您的帮助。

请按照以下代码操作:

 ch2 = [document["channel"] for document in ch]
 new_list = []

 for word in ch2[0]:
    print(word)
    if(re.findall(name, word)):
        new_list.append(word)

  return new_list

问题已经解决

你能打印并检查你在name&ch中得到的内容吗,检查这两个的类型,就像在你的另一个问题中一样,name应该是字符串&ch应该是字符串的数组/列表!!类似这样的东西::type(name),如果它们不是字符串,那么你必须转换它们,比如str(name)@srinivasy我已经在“name”中签入了我在“event”中传递的内容,它显示在“name”中,在“ch”中,数组列表显示我的意思是说你需要检查ch2中的所有值都应该是字符串&而且name应该是字符串,这就是你看到的吗?@srinivasy bro我检查了值,都是字符串。当我打印ch2时:['abc','efg','cde','xyz']。我使用ch3=(',').join(map(str,ch2)),删除了一个括号。现在我得到的数组只有一个括号。现在它工作了,但我得到的输出是这样的,当我把“c”作为输入传递时,像[“c”,“c”]这样的输出不显示匹配的整个字符串。你把整个代码放在这里,是不应该发生的,当名称是一个字符串时,对照字符串列表进行检查。。