python - 在 Python 中查找常见数字

标签 python

我有 2 个列表

a = [1,9] # signifies the start point and end point, ie numbers 1,2,3,4,5,6,7,8,9
b = [4,23] # same for this.

现在我需要确定 a 中的数字是否与 b 中的数字相交。

我可以通过制作一个来自 ab 的数字列表,然后将这两个列表相交来实现,但我正在寻找更多 pythonic 解决方案。

有没有更好的解决办法

我的 o/p 应该是 4,5,6,7,8,9

最佳答案

这是使用相交的两个列表:

c = list(set(range(a[0],a[1]+1)) & set(range(b[0],b[1]+1)))

>>> print c
[4,5,6,7,8,9]

这是使用 minmax:

>>> c = range(max([a[0],b[0]]), min([a[1],b[1]])+1)

关于python - 在 Python 中查找常见数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23053312/

相关文章:

python - 学习扭曲

python - 使用 2 位数字与 3 位数字时,django 中的数学结果不正确?

python - TensorFlow fit 给出了 TypeError : Cannot clone object error

python - 在应用程序上下文之外创建数据库

python - Matplotlib:直方图不正确

python - Python 3 中的相对导入

python - 如何转义实际上名为 <parent> 的 BeautifulSoup ISO 标签中的父属性?

python - 模块未找到错误: No module named 'application' when deploying Flask app on AWS Elastic Beanstalk

python - 如何在Python中计算多维数组的线性趋势

python - 根据多列条目过滤行