a = [1,0,2]
b = [1,0,0]
x = [k for k in a if k in b]
print(x)
結果 [1, 0]
舉例二
# 異向場景
a = [1,0,2]
b = [1,0,0]
x = [k for k in a if k not in b]
print(x)
結果:[2]
舉例三
A = [113.0, -37.02, -33, 27666.48, 0.0, 2402.35, 8290.95]
B = [113.0, -37.02, 728.55, -33, 16.5, 27666.48, 0.0, 16.89, 2402.35, 8290.95]
if [i for i in B if i in A] == A:
C = [i for i in B if i in A]
print('計算正常')
else:
C = [i for i in B if i not in A]
print('計算不正常')
print(C)
結算結果:
計算正常
[113.0, -37.02, -33, 27666.48, 0.0, 2402.35, 8290.95]
舉例四
a = [1, 2, 3]
b = [30, 50, 70]
url_1 = ['http://www.abc.gov.cn/search.jspx?page='+str(x) for x in a]
url_2 = ['&area='+str(x) for x in b]
url = [url_1[x]+url_2[x] for x in range(0,len(a))]
print(url)
結果
['http://www.abc.gov.cn/search.jspx?page=1&area=30', 'http://www.abc.gov.cn/search.jspx?page=2&area=50', 'http://www.abc.gov.cn/search.jspx?page=3&area=70']