鱼C论坛

 找回密码
 立即注册
楼主: 冬雪雪冬

[技术交流] Python:每日一题 208

[复制链接]
发表于 2018-9-13 14:29:23 | 显示全部楼层


  1. ary1=[-27,184,76,193,30,-96,186,-61,43,-94]
  2. ary2=[166,28,47,83,15,26,153,188,43,110]
  3. ary3=[]
  4. x=int(len(ary1))
  5. y=int(len(ary2))
  6. #print(x,y)
  7. i=j=m=0
  8. for i in range(0,x):
  9.     for j in range(0,y):
  10.         m = ary1[i]-ary2[j]
  11.         if m<0:
  12.             m=-m
  13.         ary3.append([m,ary1[i],ary2[j]])
  14.         #ary4.append([ary1[i],ary2[j]])
  15. #print(ary3)
  16. ary3=sorted(ary3) #按照差值进行排序
  17. print(' 第一组的数字为:',ary3[0][1],'\n','第二组的数字为:',ary3[0][2],'\n','差值的绝对值最小为:',ary3[0][0])
  18. #print(ary3[0])
复制代码


多多指教

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-13 14:31:14 | 显示全部楼层
list1 = [-62, 167, 121, 72, 57, -21, 129, 173, -80, 35]
list2 = [12, -6, 39, -11, 197, -47, -90, 28, 51, 190]
n = abs(list1[0]-list2[0])
for i in list1:
    for j in list2:
        n1 = abs(i-j)
        if n1 <= n:
            list3 = [i,j,abs(i-j)]
            n = abs(i-j)
print(list3)

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-13 15:44:30 | 显示全部楼层
  1. import numpy as np

  2. def fun208(a, b):
  3.     a = np.array(a)
  4.     diff = np.array(list(map(lambda x, y=a: np.abs(y - x), b)))
  5.     min_idx = diff.argmin()
  6.     return a[min_idx % len(a)], b[min_idx // len(a)], diff.min()
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-13 17:38:20 | 显示全部楼层
def find_some(L1,L2):
    L = []
    List = []
    s = abs(L1[0] - L2[0])
    for i in range(len(L1)):
        for j in range(len(L2)):
            b = abs(L1[i]-L2[j])
            L.append(b)
            if i == 0 and j == 0:
                list = [L1[i],L2[j],s]
                continue
            if b > min(L):
                continue
            elif b == min(L[0:len(L)-1]):
                List = List + [L1[i],L2[j],b]
            else:
                List = [L1[i],L2[j],b]
    return List

L1 = [-27, 184, 76, 193, 30, -96, 186, -61, 43, -94]
L2 = [-166, 28, 47, 83, 30, 26, 153, 188, 43, 110]
print(find_some(L1,L2))

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-13 17:56:49 | 显示全部楼层
  1. def fun_208(list1,list2):
  2.     a=list1+list2
  3.     a.sort()
  4.     b=[]
  5.     for i in range(len(a)-1):
  6.         b.append(a[i+1]-a[i])
  7.     while True:
  8.         c=b.index(min(b))
  9.         if a[c] in list1 and a[c+1] in list2:
  10.             print(a[c],a[c+1],b[c])
  11.             break
  12.         elif a[c] in list2 and a[c+1] in list1:
  13.             print(a[c+1],a[c],b[c])
  14.             break
  15.         else:
  16.             b[c]=max(b)
  17. fun_208([-27, 184, 76, 193, 30, -96, 186, -61, 43, -94],[166, 28, 47, 83, 15, 26, 153, 188, 43, 110])
  18. fun_208([-62, 167, 121, 72, 57, -21, 129, 173, -80, 35],[12, -6, 39, -11, 197, -47, -90, 28, 51, 190])            
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-13 17:59:05 | 显示全部楼层
  1. list1=[-62, 167, 121, 72, 57, -21, 129, 173, -80, 35]
  2. list2=[12, -6, 39, -11, 197, -47, -90, 28, 51, 190]
  3. list3=[]

  4. for each1 in list1:
  5.         for each2 in list2:
  6.                 list3.append(abs(int(each1)-int(each2)))

  7. print(min(list3))

  8. l1=len(list1)
  9. l2=len(list2)
  10. sub=min(list3)
  11. ind=list3.index(sub)
  12. print(list1[ind//l2])
  13. print(list2[ind%l2])
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-13 18:44:46 | 显示全部楼层
def minabs(num1,num2):
    temp=dict()
    count=[]
    for i in num1:
        for j in num2:
            value=abs(i-j)
            temp[value]=(i,j)
            count.append(value)
    count.sort()
    result=list(temp[count[0]])
    print("第一个数是%d,第二个数是%d,差的绝对值是%d."%(result[0],result[1],count[0]))



num1=[-62, 167, 121, 72, 57, -21, 129, 173, -80, 35]
num2=[12, -6, 39, -11, 197, -47, -90, 28, 51, 190]
minabs(num1,num2)   

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-13 20:45:43 From FishC Mobile | 显示全部楼层
def func208(a,b):
        m=0
        n=0
        dif=abs(a[0]-b[0])
        for i in a:
                for j in b:
                        if abs(i-j)<dif:
                                m,n,dif=i,j,abs(i-j)
        print(m,n,dif)
                               
a=[-62, 167, 121, 72, 57, -21, 129, 173, -80, 35]
b=[12, -6, 39, -11, 197, -47, -90, 28, 51, 190]
func208(a,b)

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-14 08:00:56 | 显示全部楼层
  1. def func208(lst1, lst2):
  2.     res = ''
  3.     for each1 in lst1:
  4.         for each2 in lst2:
  5.             if not isinstance(res, tuple) or res[1] > abs(each1-each2):
  6.                 res = ((each1, each2), abs(each1-each2))
  7.     return res

  8. if __name__ == '__main__':
  9.     a = [-62, 167, 121, 72, 57, -21, 129, 173, -80, 35]
  10.     b = [12, -6, 39, -11, 197, -47, -90, 28, 51, 190]
  11.     res = func208(a, b)
  12.     print(res)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-14 11:23:06 | 显示全部楼层
  1. def cha(list1,list2):
  2.     list3 = []
  3.     for i in list1:
  4.         for j in list2:
  5.             a = abs(i-j)
  6.             list3.append(a)
  7.     for m in list1:
  8.         for n in list2:
  9.             if abs(m-n) == min(list3):
  10.                 print(m,n,min(list3))
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-14 12:31:31 | 显示全部楼层
  1. def min_num(list_1,list_2):
  2.     a = list_1[0]-list_2[0] if list_1[0]>list_2[0] else list_2[0]-list_1[0]
  3.     m = 0
  4.     n = 0
  5.     for num_i,i in enumerate(list_1):
  6.         for num_j,j in enumerate(list_2):
  7.             b = i-j if i>j else j-i
  8.             if b<a:
  9.                 m = num_i
  10.                 n = num_j
  11.                 a = b

  12.     return(list_1[m],list_2[n],a)
复制代码

  1. >>> list_1 = [-27, 184, 76, 193, 30, -96, 186, -61, 43, -94]
  2. >>> list_2 = [166, 28, 47, 83, 15, 26, 153, 188, 43, 110]
  3. >>> print(min_num(list_1,list_2))
  4. (43, 43, 0)
  5. >>> a = [-62, 167, 121, 72, 57, -21, 129, 173, -80, 35]
  6. >>> b = [12, -6, 39, -11, 197, -47, -90, 28, 51, 190]
  7. >>> print(min_num(a,b))
  8. (35, 39, 4)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-14 16:23:53 | 显示全部楼层


  1. def do(_List1,_List2):
  2.     tem = []
  3.     _val1= 0
  4.     for _each1 in _List1:
  5.         for _each2 in _List2:
  6.             _val2 = abs(_math (int(_each1),(_each2)))
  7.             if _val2 <= _val1:
  8.                 tem = []               
  9.                 tem.append(_each1)
  10.                 tem.append(_each2)
  11.                 tem.append(_val2)
  12.             else:
  13.                 pass
  14.     print(tem)
  15.             
  16.             


  17. def _math(num_1,num_2):
  18.     value = num_1 - num_2
  19.     return value
复制代码

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
冬雪雪冬 + 1 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-14 21:53:54 | 显示全部楼层
  1. def mina():
  2.     list1 = [-62, 167, 121, 72, 57, -21, 129, 173, -80, 35]
  3.     list2 = [12, -6, 39, -11, 197, -47, -90, 28, 51, 190]
  4.     dict1 = dict()

  5.     for each_1 in list1:
  6.         for each_2 in list2:
  7.             re = abs(each_1 - each_2)
  8.             dict1[(each_1, each_2)] = re
  9.     list3 = list(dict1.values())

  10.     a = list3.index(min(list3))
  11.     list4 = list(dict1.keys())

  12.     print('%d,%d,%d' % (list4[a][0], list4[a][1],min(list3)))

  13. mina()
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-15 20:40:01 | 显示全部楼层
  1. def f208(a,b):
  2.     he=99
  3.     for i in a:
  4.         for x in b:
  5.             js=abs(i-x)
  6.             if js<he:
  7.                 he=js
  8.                 result=[i,x,he]
  9.     return result
  10. a=[-27, 184, 76, 193, 30, -96, 186, -61, 43, -94]
  11. b=[166, 28, 47, 83, 15, 26, 153, 188, 43, 110]
  12. print(f208(a,b))
  13. a=[-62, 167, 121, 72, 57, -21, 129, 173, -80, 35]
  14. b=[12, -6, 39, -11, 197, -47, -90, 28, 51, 190]
  15. print(f208(a,b))
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-17 10:22:35 | 显示全部楼层
本帖最后由 大头目 于 2018-9-17 10:24 编辑
  1. def a_b_abs(list1,list2):
  2.         r = abs(list1[0]-list2[0])
  3.         for x in list1:
  4.                 for y in list2:
  5.                         if abs(x - y) < r:
  6.                                 a = x
  7.                                 b = y
  8.                                 r = abs(x -y)
  9.         return a,b,r


  10. L1 = [-27, 184, 76, 193, 30, -96, 186, -61, 43, -94]
  11. L2 = [166, 28, 47, 83, 15, 26, 153, 188, 43, 110]
  12. print(a_b_abs(L1,L2))


  13. L3 = [-62, 167, 121, 72, 57, -21, 129, 173, -80, 35]
  14. L4 = [12, -6, 39, -11, 197, -47, -90, 28, 51, 190]
  15. print(a_b_abs(L3,L4))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-17 10:34:53 | 显示全部楼层
  1. def fun208(x,y):
  2.     for i in x:
  3.         for j in y:
  4.             try:
  5.                 if abs(i-j) < result:
  6.                     result_x,result_y,result = i,j,abs(i-j)
  7.             except NameError:
  8.                 result_x,result_y,result = i,j,abs(i-j)
  9.     return '%s,%s,%s'%(result_x,result_y,result)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-17 17:59:18 | 显示全部楼层
写得太麻烦了,看来自己在这方面还很薄弱,以后要多多加强~

  1. def fun208(list1, list2):
  2.     result1, result2 = [], []
  3.     for i, num1 in enumerate(list1):
  4.         d = [abs(num1 - num2) for num2 in list2]
  5.         m = min(d)
  6.         result1.append([list1[i], list2[d.index(m)]])
  7.         result2.append(m)

  8.     k = result2.index(min(result2))
  9.     result1[k].append(min(result2))
  10.     print(result1[k])


  11. l1 = [-62, 167, 121, 72, 57, -21, 129, 173, -80, 35]
  12. l2 = [12, -6, 39, -11, 197, -47, -90, 28, 51, 190]
  13. fun208(l1, l2)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-17 19:28:31 | 显示全部楼层
吴昌汶 发表于 2018-9-13 11:27
n = int(input('请输入第一组数的个数'))
a = []
for i in range(n):

哈哈哈哈哈 尴尬,那个是后面想着完善,加上去的,没跑。。。果然有问题。。。sorry
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-18 15:23:43 | 显示全部楼层
  1. def practice_208(list1,list2):
  2.     # 初始化,产生一个最大的数,以此为基点
  3.     list_number = [max(list1),min(list2)]
  4.     list_temp =[abs(list_number[0] - list_number[1])]
  5.     for i in list1:
  6.         for j in list2:
  7.             if abs(i-j)< min(list_temp):
  8.                 list_number[0] = i
  9.                 list_number[1] = j
  10.                 list_temp.append(abs(i-j))
  11.     print(list_number,abs(list_number[0]-list_number[1]))

  12. if __name__ == '__main__':
  13.     list1 = [-27, 184, 76, 193, 30, -96, 186, -61, 43, -94]
  14.     list2 = [166, 28, 47, 83, 15, 26, 153, 188, 43, 110]
  15.     list3 = [-62, 167, 121, 72, 57, -21, 129, 173, -80, 35]
  16.     list4 = [12, -6, 39, -11, 197, -47, -90, 28, 51, 190]
  17.     practice_208(list1,list2)
  18.     practice_208(list3,list4)
  19.    
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-18 21:24:06 | 显示全部楼层

代码简化版本
  1. def fun_208(list1,list2):
  2.     a=sorted(list1+list2)
  3.     b=list(map(lambda x,y:x-y,a[1:],a[:-1]))
  4.     while True:
  5.         c=b.index(min(b))
  6.         if (a[c] in list1 and a[c+1] not in list2) or (a[c] in list2 and a[c+1] not in list1):
  7.             b[c]=max(b)
  8.         else:
  9.             print(a[c],a[c+1],b[c]) if a[c] in list1 and a[c+1] in list2 else print(a[c+1],a[c],b[c])            
  10.             break
  11. fun_208([-27, 184, 76, 193, 30, -96, 186, -61, 43, -94],[166, 28, 47, 83, 15, 26, 153, 188, 43, 110])
  12. fun_208([-62, 167, 121, 72, 57, -21, 129, 173, -80, 35],[12, -6, 39, -11, 197, -47, -90, 28, 51, 190])     
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 18:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表