简体   繁体   English

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

[英]How to find strings with matched letters in list/array using lambda function?

I am using lambda function, python 3.6 and Mongodb atlas.我正在使用 lambda function、python 3.6 和 ZC1F54D1681966E839EAE45D5846 In mongodb i have one collection below like this.在 mongodb 中,我在下面有一个这样的集合。 collection name profile.集合名称配置文件。 below as the collection structure.下面作为集合结构。

"_id" : ObjectId("5db234df92b0ce00016932f3")
"username" : "testing"
"channel" : [ "abc", "efg", "cde", "xyz" ]
"about" : "this is a test case"

we have multiple rows similar to the above.我们有多个类似于上面的行。 Now i am using python, i write the lambda function to find strings matched letter in channel array.find the below lambda function. Now i am using python, i write the lambda function to find strings matched letter in channel array.find the below lambda function.

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

I have passed "cname": "c" in event.我在事件中通过了“cname”:“c”。 but i am getting error like this.但我收到这样的错误。

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)"
]
]
}

I tried with re.search also but i am getting same, I need output like this below.我也尝试使用 re.search,但我得到了相同的结果,我需要 output,如下所示。

Input: "cname" : "c"
output: "abc"
        "cde"

can you please help me with solution, thanks on advance.你能帮我解决一下吗,在此先感谢。

Follow the below code:按照下面的代码:

 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

the issue has been resolved问题已解决

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

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