Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.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中的for和if循环实现匹配条件_Python_Json_Loops_For Loop_If Statement - Fatal编程技术网

用python中的for和if循环实现匹配条件

用python中的for和if循环实现匹配条件,python,json,loops,for-loop,if-statement,Python,Json,Loops,For Loop,If Statement,我有一个json数据,如下所示,每个字段中都有一个uri要匹配。 如果找到匹配项,则不要进行[字段]的下一次迭代。 概念是用户在搜索后单击了URL。 如果用户单击了第三个url,则收集第一、第二、第三个数据值。不要选择第四值 若用户单击了第5个url,则收集第1到第5个字段uri并退出该json对象。 获取一个新的json对象并执行相同的过程 [document]-> [fields1] -> [uri] [document]-> [fields2] -> [uri] [

我有一个json数据,如下所示,每个字段中都有一个uri要匹配。 如果找到匹配项,则不要进行[字段]的下一次迭代。 概念是用户在搜索后单击了URL。 如果用户单击了第三个url,则收集第一、第二、第三个数据值。不要选择第四值

若用户单击了第5个url,则收集第1到第5个字段uri并退出该json对象。 获取一个新的json对象并执行相同的过程

[document]-> [fields1] -> [uri]
[document]-> [fields2] -> [uri]
[document]-> [fields3] -> [uri]
..... 
.. till 20-30 times. 

    I have written below code, but the above logic is not working. Kindly help on this. 



 uri='http://abcd.com/123.html'
    print(uri)
    for index_srch_log,row_srch_log in df_search_log_mongo.iterrows():
        RESPONSE = row_srch_log['RESPONSE']
        json_response = json.loads(RESPONSE)
        if 'documents' in json_response:
            field_data=json_response['documents']

            for row_resp_list in field_data:
                print('uri:',row_resp_list['fields']['uri'])
                match_found=False
                for i in row_resp_list['fields']['uri']:
                    print('i',i)

                    if uri == i:
                        print('yes matched')
                        match_found=True
                        break
                        print('found')
                    else:
                        print('not matched')
                        match_found=False
                    if match_found==True:
                        break
输出:

uri: ['http://abcddsc779072.html']
i value: http://abcddsc779072.html
not matched
uri: ['http://abcddsc932618.html']
i value: http://abcddsc932618.html
yes matched
--它应该停在这里,从DF获取下一个响应对象。 --但接下来的[字段]数据仍在继续

  uri: ['http://abcddsc988555.html']
    i value:  http://abcddsc988555.html
    not matched
    uri: ['http://abcddsc1094909.html']
    i value: http://abcddsc1094909.html
    not matched

你没有打破外环。考虑以下变化:

    if 'documents' in json_response:
        field_data=json_response['documents']

        for row_resp_list in field_data:
            print('uri:',row_resp_list['fields']['uri'])
            match_found=False
            for i in row_resp_list['fields']['uri']:
                print('i',i)

                if uri == i:
                    print('yes matched')
                    match_found=True
                    break
                else:
                    print('not matched')

            if match_found:
                break

你没有打破外环。考虑以下变化:

    if 'documents' in json_response:
        field_data=json_response['documents']

        for row_resp_list in field_data:
            print('uri:',row_resp_list['fields']['uri'])
            match_found=False
            for i in row_resp_list['fields']['uri']:
                print('i',i)

                if uri == i:
                    print('yes matched')
                    match_found=True
                    break
                else:
                    print('not matched')

            if match_found:
                break

你得到的是什么样的输出?我得到的是字段的所有迭代。即使在找到匹配项之后。它应该在“是”匹配后停止。uri:['我不匹配的uri:['我是匹配的uri:['我不匹配的uri:['我不匹配的uri:['你得到了什么样的输出?我得到了字段的所有迭代。即使在找到匹配之后。它应该在是匹配后停止。uri:['我不匹配的uri:['我是匹配的uri:['我不匹配的uri:[“我没有那么感谢你。你太棒了。工作得很有魅力。哦,从来没有得到过这样的感激….)我的荣幸!非常感谢你。你太棒了。工作得很有魅力。哦,从来没有得到过这样的感激….)我的荣幸!