How to find strings with matched letters in list/a

2020-05-08 07:15发布

I am using lambda function, python 3.6 and Mongodb atlas. In mongodb i have one collection below like this. 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.

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. 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.

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

can you please help me with solution, thanks on advance.

1条回答
相关推荐>>
2楼-- · 2020-05-08 08:04

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

查看更多
登录 后发表回答