Py学习  »  Python

Python——从一个列表切换到另一个列表(最佳方式)

VeljkoSbbb • 3 年前 • 1195 次点击  

我有两份清单

list1 = [1, 3, 5, 8]
list2 = [7, 10, 12]

还有一些破坏int值 switchValue = 4 我需要反复浏览 list1 而其元素的价值小于 switchValue ,然后迭代整个 list2 1.我知道如何使用 if break 声明 else 分支,但我正在研究如何在Python2.7中实现这一点的一些更优化(推荐)的方法,因为这些列表将非常大。此外,还会对列表进行排序。

result 应该是 1, 3, 7, 10, 12

list1 = [1, 3, 5, 8]
list2 = [7, 10, 12]
switchValue = 4
for i in list1:
    if i < switchValue:
        print(i)
    else:
        for j in list2:
            print(j)
        break
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131527
 
1195 次点击  
文章 [ 2 ]  |  最新文章 3 年前