Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.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 如果列表没有';找不到与列表匹配的内容?_Python_For Loop_While Loop - Fatal编程技术网

Python 如果列表没有';找不到与列表匹配的内容?

Python 如果列表没有';找不到与列表匹配的内容?,python,for-loop,while-loop,Python,For Loop,While Loop,当我的代码在整个列表中找不到任何内容时,我想进行打印 这是我的代码: new_list = ['There%is', 'None&of', 'Same&here'] a = new_list.split("%") old_list = ['Stack%Hello', 'Over&You', 'flow&There'] condition = True while condition: try: for i, items in zip(ran

当我的代码在整个列表中找不到任何内容时,我想进行打印

这是我的代码:

new_list = ['There%is', 'None&of', 'Same&here']
a = new_list.split("%")
old_list = ['Stack%Hello', 'Over&You', 'flow&There']
condition = True
while condition:
    try:
        for i, items in zip(range(len(old_list)), old_list):
            old_list_value = old_list[i].split("%")
            if a[0] in items:
                if len(old_list_value[1]) < len(a[1]):
                    old_list[i] = new_list        
                    condition = False

                elif len(old_list_value[1]) > len(a[1]):
                    old_list[i] = new_lis
                    condition = False

            else:
                old_list.append(new_list)
                condition = False
                break

    except Exception as err:
        print(err)

您可能需要查看以下内容以查找其他内容:

for item in items:
    if you_found_something:
        found_item = item
        break
else:
    print('No item found.')
    do_your_append_here

基本上,for-else回答了for循环是否在没有中断的情况下终止的问题。在上面,当我们发现一些东西时,我们会中断,
else
将不会执行,但是只要for循环在没有中断语句的情况下终止,那么
else
中的代码将被执行。

在这种情况下,我不需要实际执行while循环和我所做的事情?这是正确的。你可能想用这种新的语法重新构造你的解决方案,这应该可以解决你的问题。
for item in items:
    if you_found_something:
        found_item = item
        break
else:
    print('No item found.')
    do_your_append_here