iltifa
发表于 2020-3-12 11:01:27
def test17(my_list):
min_list = []
for i in range(len(my_list) - 1):
n = abs(my_list - my_list)
min_list.append(n)
min_value = min(min_list)
return min_value
a =
print(test17(a))
>>>9
mcx88
发表于 2020-3-15 12:05:53
#求两两相差的绝对值,需要的次数为(n-1)*n/2
import random as r
def test17(my_list):
the_list = []
geshu=len(my_list)
for i in range(geshu-1):
for j in range(1+i,geshu):
n=my_list-my_list
the_list.append(abs(n))
return min(the_list)
a = #在这里可改为指定的数组
print(a)
print()
print('两两之差绝对值最小的值是:%d'%test17(a))
魔法鳕鱼
发表于 2020-3-15 12:32:03
def func(list):
varlist = []
for x in range(len(list)):
for y in range(len(list)):
if x == y:
continue
else:
varlist.append(list-list)
for i in range(len(varlist)):
if varlist <= 0:
varlist = -varlist
min = varlist
for i in varlist:
if min >= i:
min = i
return min
mcx88
发表于 2020-3-15 12:37:58
#换个思路感觉需要的次数更少。只需要比较n-1次
import random as r
a=#这里也可以换成指定的数组
print(a)
b=a[:];c=[]
b.sort(reverse=True)
for i in range(len(b)-1):
c.append(b-b)
print(min(c))
请大佬多多帮助,小可是个新手,很多都不了解。{:10_254:}
holiday_python
发表于 2020-3-31 13:51:14
import random
def min_num(array):
num = []
for i in range(len(array)):
for j in range(1,len(array)):
if array != array:
num.append(abs(array - array))
num.sort()
min_result = num
print('两数之差最小值为%d' % min_result)
array =
print(array)
min_num(array)
听风夜雨
发表于 2020-4-6 10:49:03
iimport numpy as np
def f(x):
y=[]
for i in range(len(x)):
for j in range(len(x)):
if i!=j and x-x>=0:
y.append(x-x)
y.sort()
return y
#例子
x=
y=np.random.randint(1,100,5)
print(f(x),f(y),sep='\n')
君子好逑
发表于 2020-4-12 23:27:01
import random
n=input("请输入数组的元素个数:")
n=int(n)
a=[]
for each in range(0,n):
b=random.randint(0,50)
a.append(b)
for each in range(0,n-1):
if(each==0):
n=a-a
if(n<0):
n=-n
else:
m=a-a
if(m<0):
m=-m
elif(m<n):
n=m
print(n)
永恒的蓝色梦想
发表于 2020-4-12 23:52:47
看看
Debugme
发表于 2020-4-13 11:27:07
本帖最后由 Debugme 于 2020-4-13 11:33 编辑
array =
dif = []
i = 1
for num1 in array:
array_temp = array
for num2 in array_temp:
temp = abs(num1 - num2)
dif.append(temp)
i += 1
dif_min = min(dif)
dif_max = max(dif)
print("最小值是:%d,最大值是:%d"%(dif_min,dif_max))
#萌新的答案
把最大值和最小值都算出来了
为啥你们的函数命名是foo呢
尔东方
发表于 2020-5-1 18:28:01
本帖最后由 尔东方 于 2020-5-1 18:30 编辑
List=
print(List)
L=[]
for i in range(len(List)-1):
for j in range(i+1,len(List)):
x=abs(List-List)
L.append(x)
print(min(L))
永恒的蓝色梦想
发表于 2020-5-1 19:20:38
新手·ing 发表于 2017-4-3 15:37
再多学习吧,我也是新手的
新——手——{:10_247:}
Balaenopter
发表于 2020-5-3 08:59:52
string1 = input('num:')
list1 = list(string1.split(','))
m = abs(int(list1) - int(list1))
for i in list1:
for j in list1:
num1 = int(i)
num2 = int(j)
if num1 != num2 and abs(num1 - num2) < m:
m = abs(num1 - num2)
print(m)
san1412
发表于 2020-5-17 08:30:19
list1 = list(int(i) for i in input('请输入一组数字,用逗号隔开: ').split(','))
list2 = list1
list3 = []
for n1 in list2:
n2 = list2.index(n1)
del list2
for n3 in list2:
list3.append(abs(n1-n3))
list2 = list1
print('两两之差绝对值最小的值为:',min(list3))
开心果.
发表于 2020-5-17 19:15:06
本帖最后由 开心果. 于 2020-5-17 19:32 编辑
def sur(list1):
length = len(list1)
list2 = []
num = 0
for i in list1:
for each in range(length):
if each != num:
sur = i - list1
list2.append(sur)
num += 1
for each in range(len(list2)):
if list2<0:
list2 = abs(list2)
print(list2)
print(min(list2))
list1 =
sur(list1)
结果如下:
0
开心果.
发表于 2020-5-17 19:35:19
开心果. 发表于 2020-5-17 19:15
def sur(list1):
length = len(list1)
list2 = []
刚没看到绝对值
今天的我更强了
发表于 2020-6-29 09:24:09
sy=
length=len(sy)
list1=[]
for i in range(length):
for j in range(i+1,length):
num=abs(sy-sy)
list1.append(num)
print(min(list1))
nllcywl
发表于 2020-6-29 10:04:44
666
鲨鱼辣椒1
发表于 2020-8-10 11:22:14
def number(a): # 数组就是列表
list1 = []
a.sort()
for i in range(len(a) - 1):
result = abs(a - a)
list1.append(result)
print(min(list1))
a =
number(a)
xixihaha1235
发表于 2020-8-31 15:57:02
def foo(data):
b=[]
le = len(data)
for i in range(0,le):
b.append(data)
min=abs(data-b)
for i in range(le):
for j in range(le):
if i == j:
break
if abs(data-b)< min:
min=abs(data-b)
return min
a=
print(foo(a))
yangchen666
发表于 2020-8-31 17:34:36
啊这