捞鬼
发表于 2019-3-25 20:54:03
莫名其妙短路
yu123py
发表于 2019-4-28 10:46:06
有谁来帮我看看代码里有没有什么不妥吗?
import random
l =
l.sort()
print(l)
l1 = []
for j in range(len(l)-1):
l1.append(l - l)
print(min(l1))
wovulizhil
发表于 2019-5-22 16:49:57
import numpy as np
array = []
for i in range(np.random.randint(1,100)): #####随机取一个数组
array.append(random.uniform(1,100))
array = np.array(array)
Arr = []
def iter1(array,count,length):
global value
if count != length:
for i in range(length):
if count == i:
continue
value = abs(array - array)
Arr.append(value)
count +=1
return iter1(array,count,length)
else:
z= min(Arr)
return z
min_value = iter1(array , count=0,length=len(array))
panheng
发表于 2019-8-13 17:22:20
import random as r
# 解法1:双层循环并计算两数差求绝对值,加入列表后调用min函数求解
def answer1():
list2 = []
for each in list1:
for index in range(list1.index(each)+1, len(list1)):
result = abs(each - list1)
list2.append(result)
print(min(list2))
# 解法2:双层循环并计算两数差求绝对值,加入集合后调用min函数求解
def answer2():
set1 = set()
for each in list1:
for index in range(list1.index(each) + 1, len(list1)):
result = abs(each - list1)
set1.add(result)
print(min(set1))
#解法3:将列表数据排序后,迭代求出前后项之间的距离(即绝对值),选出最近的两个点即为所求
def answer3():
list1.sort()
m = min( - list1) for i in range(len(list1) - 1)])
print(m)
if __name__ == '__main__':
list1 = #生成包含20个-50到100随机数的列表
print(list1)
answer1()
print("------------")
answer2()
print("------------")
answer3()
print(list1)
juzhong0607
发表于 2019-8-14 14:38:58
还可以把这两个数打印出来:
def fn(lst):
a = {}
for i in range(0, len(lst) - 1):
for j in range(i + 1, len(lst)):
a, lst] = abs(lst - lst)
print(a)
for k, v in a.items():
if v == min(a.values()):
print(k, v)
lst =
fn(lst)
TomasZ
发表于 2019-8-15 11:25:29
good
克里斯保罗
发表于 2019-8-29 12:36:06
交作业~def func(data):
temp = abs(data-data)
for i in range(1,len(data)-1):
min_num = abs(data-data)
if temp > min_num:
temp = min_num
return temp
def main():
data = input('请输入一组数组:').split(',')
for each in range(len(data)):
data = int(data)
min_num = func(data)
print('最小的值是%d'%min_num)
if __name__ =='__main__':
main()
Jung
发表于 2019-11-27 09:05:31
def fun17(X): #X定义数组长度
L = []
Min = []
for i in range(X):
L.append(int(input("Input integer data for list element:")))
L.sort()
for k in range(len(L)-1):
Min.append(L-L)
Min.sort()
return Min
qq614704680
发表于 2020-2-8 20:55:32
def abs_num(a = []):
b = 0
c = set()
if len(set(a)) < len(a):
return 0
else:
for i in range(len(a)):
for j in a+a:
c.add(abs(a -j))
b += 1
return min(c)
a = input('Enter:')
a = a.split(',')
alist = []
for i in a:
alist.append(int(i))
print(abs_num(alist))
ouyunfu
发表于 2020-2-13 00:50:32
def f17(L:list)->int:
L1=sorted(L)
return min(-L1 for i in range(1,len(L))])
qdrelang
发表于 2020-2-15 21:06:26
{:5_109:}
qdrelang
发表于 2020-2-15 22:08:33
def qmin(data):
data.sort()
min=data-data
for i in range(1,len(data)-1):
if min>data-data:
min=data-data
return min
a=
qmin(a)
thXnder
发表于 2020-2-18 16:08:19
def smallest(list_integer):
smallest=abs(list_integer-list_integer)
for each1 in list_integer:
for each2 in list_integer:
if each1!=each2:
if abs(each1-each2)<smallest:
smallest=abs(each1-each2)
return smallest
print()
xuanyuan0116
发表于 2020-2-24 16:16:36
print('----------每日练习题17----------')
#有一个整数数组,请求出两两之差绝对值最小的值,
#记住,只要得出最小值即可,不需要求出是哪两个数
def fun(x,y):
minnum = abs(x - y)
for i in x:
for j in y:
if abs(i - j) < minnum:
minnum = abs(i - j)
return minnum
import random as rd
x = []
y = []
for i in range(10):
x.append(rd.randint(-10000,10000))
x.append(rd.randint(-10000,10000))
print(x)
for j in range(10):
y.append(rd.randint(-10000,10000))
y.append(rd.randint(-10000,10000))
print(y)
print(fun(x, y))
海上飞鱼
发表于 2020-3-1 10:55:14
print(min(-num) for i in range(len(num)) for j in range(i+1,len(num))]))
catwine1990
发表于 2020-3-8 23:13:34
#有一个整数数组,请求出两两之差绝对值最小的值
dlt=[]
test=
x.sort()
for i in range(len(x)-1):
abs1=abs(x-x)
dlt.append(abs1)
print('数组两两之差绝对值最小为%d'%min(dlt))
怎么读取用户输入的列表搞不动
杜若左
发表于 2020-3-12 09:40:47
嘻嘻j = []
k = int(input("这个列表有有几个数呢?"))
for i in range(k):
j.append(int(input()))
min = abs(j-j)
for i in range(k):
for p in range(i+1,k):
if abs(j-j)<min:
min = abs(j-j)
print(min)
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