python intersection.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python intersection.py相关的知识,希望对你有一定的参考价值。

from random import randint

def random_array(n):
	f = lambda i : randint(10, 100)
	return map(f, range(n))

A = sorted(random_array(10))
B = sorted(random_array(10))

def intersection(A, B):
	common = []
	i = j = 0
	while i < len(A) and j < len(B):
		if A[i] < B[j]:
			i += 1
		elif A[i] > B[j]:
			j += 1
		else: #common
			common_element = A[i]
			common.append(common_element)
			while i < len(A) and A[i] == common_element:
				i += 1
			while j < len(B) and B[j] == common_element:
				j += 1
	return common


print A
print B
print intersection(A, B)

以上是关于python intersection.py的主要内容,如果未能解决你的问题,请参考以下文章

python vert2intersection.py

代写python,代写python编程,python代写,python编程代写,留学生python代写

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python