如果第二遍如何遍历列表

时间:2020-08-07 04:52:28

标签: python list

我有一个清单。我需要传递event ['params'] ['path'] ['match'] == i

event = {'body-json': {}, 'params': {'path': {'match': '%20'}, 'querystring': {}, 'header': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Content-Type': 'application/json', 'Host': 'xx.amazonaws.com', 'Postman-Token': '50ae98db-6077-47c4-ac11-bfc53ac78990', 'User-Agent': 'PostmanRuntime/7.26.2', 'X-Amzn-Trace-Id': 'Root=1-5f2cb950-27ff7e6789988bd6b1a74d76', 'X-Forwarded-For': 'xxx.24', 'X-Forwarded-Port': '443', 'X-Forwarded-Proto': 'https'}}, 'stage-variables': {}, 'context': {'account-id': '', 'api-id': 'xx', 'api-key': '', 'authorizer-principal-id': '', 'caller': '', 'cognito-authentication-provider': '', 'cognito-authentication-type': '', 'cognito-identity-id': '', 'cognito-identity-pool-id': '', 'http-method': 'POST', 'stage': 'v1', 'source-ip': 'xx.24', 'user': '', 'user-agent': 'PostmanRuntime/7.26.2', 'user-arn': '', 'request-id': '8d13e523-de12-4527-b58d-b11a7c8d09fb', 'resource-id': 'td4vr4', 'resource-path': '/{match}'}}

代码在下面

list_ = [',' , '.' , '$' , ' ']
for i in list_:
    if bool(event['params']['path'])== False or event['params']['path']['match'] == i:
          print ('test success')

基本上我需要遍历列表_

event['params']['path']['match'] == i

我不想做下面的事情

if bool(event['params']['path'])== False or event['params']['path']['match'] == $ \ 
              or event['params']['path']['match'] == .  or event['params']['path']['match'] == , :
              print ('test success')

2 个答案:

答案 0 :(得分:1)

您可以使用if event['params']['path']['match'] in list_条件

答案 1 :(得分:1)

list_ = [',' , '.' , '$' , ' ']
for i in list_:
    if bool(event['params']['path'])== False or event['params']['path']['match'] in list_:
          print ('test success')