MST

星途 面试题库

面试题:Python中基于多个列表的条件判断基础应用

假设有两个列表list1 = [1, 2, 3, 4] 和 list2 = [3, 4, 5, 6],编写Python代码判断是否存在一个数,它既在list1的前半部分(这里前半部分定义为长度整除2的部分),又在list2的后半部分(这里后半部分定义为从长度整除2开始到结尾的部分)。如果存在,打印出这个数;如果不存在,打印提示信息 '不存在符合条件的数'。
47.7万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
half_len1 = len(list1) // 2
half_len2 = len(list2) // 2
found = False
for num in list1[:half_len1]:
    if num in list2[half_len2:]:
        print(num)
        found = True
        break
if not found:
    print('不存在符合条件的数')