列表理解的结果错误
Error in the results of a list comprehension
我对列表推导有疑问。
import numpy
import random
diam=1.5
p=1
a=10
d=0.2
h=0.15
lx = list(numpy.arange(-diam/2,diam/2+0.05,0.05))
for loop in range(50):
f=random.uniform(0,p/2)
l = [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)<-d/2-h/np.tan(np.pi/2-a))] + [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a))>(d/2)]
但是当我查看结果时,似乎有一些错误。
对于 a=10
,对于 f
的最后一个值,
l = [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
然后如果我输入这个
[k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a) for k in list(np.arange(-diam/2,diam/2+0.05,0.05))]
我明白了
[-0.72926117723233752, -0.68565303028301938, -0.64160406588192997, -0.59711428402906952, -0.54781631527556185, -0.49318773203196459, -0.43899996624013848, -0.38525301790008354, -0.33194688701179981, -0.27908157357528718, -0.22665707759054576, -0.17467339905757551, -0.12313053797637638, -0.072028494346948441, -0.021367268169291655, 0.028853140556593968, 0.07863273183070843, 0.12797150565305171, 0.17686946202362386, 0.22532660094242482, 0.27334292240945463, 0.32091842642471324, 0.36805311298820076, 0.41474698209991706, 0.4610000337598622, 0.50681226796803625, 0.55218368472443902, 0.60288571597093177, 0.65839593411807129, 0.71434696971698208, 0.77073882276766403]
(我将其称为 mylist
)
我知道 -d/2-h/np.tan(np.pi/2-a) = -0.12644904710626975
和 d/2 = 0.1
然后,如果我查看 mylist
中的值,我会注意到前 12 个值不如 -0.12644904710626975
。
但是,在l
中,k
只有11个连续值(-0.15
也应该在l
中)。
我不明白为什么 -0.15
不在列表 l
中。
编辑:这是另一个示例
l = [-0.75, -0.7 , -0.65, -0.6 , -0.55, -0.5 , -0.45, -0.4 , -0.35,
-0.3 , -0.25, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 ,
0.45, 0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75]
mylist = [-0.73693754824886837, -0.68054569519818642, -0.62459465959927574, -0.5690844414521361, -0.51401504075676774, -0.45938645751317042, -0.40519869172134437, -0.35145174338128943, -0.29814561249300564, -0.24528029905649307, -0.19285580307175165, -0.14087212453878137, -0.089329263457582256, -0.038227219828154309, 0.012434006349502473, 0.06265441507538809, 0.11243400634950255, 0.16177278017184585, 0.21067073654241797, 0.25912787546121896, 0.30714419692824874, 0.35471970094350741, 0.40185438750699487, 0.44854825661871117, 0.49480130827865632, 0.54061354248683036, 0.58598495924323313, 0.63091555854786485, 0.67540534040072531, 0.7194543048018146, 0.76306245175113285]
mylist
的前12个值不如-0.12644904710626975
,但在l
中,只有11个连续值:-0.2
应该在[=21中=] 也是。
(验证: k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)
for k = -0.2
, 我们得到 -0.1408721245387819
不如 -0.12644904710626975
)
好吧,我检查的方式 - 看起来不错。你能提供不同的样本吗,我很乐意研究它。
import numpy as np
import random
diam = 1.5
p = 1
a = 10
d = 0.2
h = 0.15
lx = list(np.arange(-diam/2, diam/2 + 0.05, 0.05))
for loop in range(50):
f = random.uniform(0, p/2)
a1 = [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)<-d/2-h/np.tan(np.pi/2-a))] + [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a))>(d/2)]
a2 = [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)<-d/2-h/np.tan(np.pi/2-a))]
a3 = [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a))>(d/2)]
a4 = [k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a) for k in list(np.arange(-diam/2,diam/2+0.05,0.05))]
print "lx", lx
print "a1", a1
print "a2", a2
print "a3", a3
print "a4", a4
# however...
print -d/2-h/np.tan(np.pi/2-a)
print k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)
print -0.19999999999999951 < -d/2-h/np.tan(np.pi/2-a)
print -0.14999999999999947 < -d/2-h/np.tan(np.pi/2-a)
# EDIT2 => addition, see explanation below.
print "EDIT 2 results step by step equation from comment below answer"
print "==========================================="
x1 = (1/(2*p))
print "x1 = ", x1
x2 = (0.05**2)
print "x2 = ", x2
print "abs(f-x1*x2) = ", abs(f-x1*x2)
print "np.tan(a) = ", np.tan(a)
print "abs(f-x1*x2) * np.tan(a) = ", abs(f-x1*x2) * np.tan(a)
print "abs(f-x1*x2) * np.tan(a) + 0.05 = ", 0.05 + abs(f-x1*x2) * np.tan(a)
print "==========================================="
输出
lx [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, -0.14999999999999947, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
a1 [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
a2 [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951]
a3 [0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
a4 [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, -0.14999999999999947, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
-0.197254124119
0.75
True
False
EDIT 2 results step by step equation from comment below answer
===========================================
x1 = 0
x2 = 0.0025
abs(f-x1*x2) = 0.0
np.tan(a) = 0.648360827459
abs(f-x1*x2) * np.tan(a) = 0.0
abs(f-x1*x2) * np.tan(a) + 0.05 = 0.05
===========================================
编辑,回应@Jack 的评论和post 编辑。
@Jack - 您在编辑中提供的示例仍然不清楚。但首先:a4 和 lx 值相同,请随意将此 post 复制到新脚本并自行测试。您确定 post 中的值正确吗?您指的是 0.12644904710626975,而我的示例明确指出:
-d/2-h/np.tan(np.pi/2-a) != -0.12644904710626975
-d/2-h/np.tan(np.pi/2-a) == -0.197254124119
也许您在程序的其他地方更改了 d/h/a。
你如何分配一些变量来代替像这样的长结构,它们很难阅读并且可能会导致错误。
我也不知道,您在编辑中用来生成值的参数是什么。
也许你可以写下你想要实现的目标,这样解决问题可能更容易。
编辑 2:
在您下面的评论中您写道:
a4 and lx are not the same, I just tried and I get lx = [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, (...)
whereas a4 = [-0.67386015491279805, -0.64736323490818992, -0.61924541283493406, -0.58950668869303047, -0.54185293751752051, -0.47483346579671942, (...)
The value seems to be -0.197254124119. Sorry, I probably mixed different values... Anyway, for d/2 = 0.1, you can see that the last 15 values of mylist in the EDIT example are superior to 0.1, but there are only 14 consecutive values for the last values of l
And 0.05+(abs(f-(1/(2*p))*(0.05**2)))*np.tan(a) = 0.15540118660134225 > 0.1. So why 0.05 isn't in the list l ?
你有没有复制上面的脚本并在你的电脑上试过,和上面写的一模一样?我在上面添加了另外几行,以“==========”开头和结尾,以演示我从计算中获得的逐步结果。请注意,diam、p、a、d、h 在任何地方都没有改变。下面我写了原因 - 为什么会发生这种情况以及您可以做些什么来避免这种行为。
摘要
现在我们真正看到发生了什么。请观察 x1 得到了什么值。我们有 x1 == 0 而我们(作为人类)清楚地看到 1/(2*1) 是 0.5。
您可能知道为什么会发生这种情况,但如果您不知道,计算机就不会算作我们 - 它在 Python2 中称为整数除法(在 Python3、2/2 = 1.0
中已更改) ,所以在 Python2 "normal" 除法运算符“/”除以整数,而“//”除以浮点数。其他一些方法也是可能的,像这样 float(1)/2 == 0.5
或使用语句 from __future__ import division
将其中一个操作数转换为除法之前的浮点数。我很确定你也可以像这样 1.0/2 == 0.5
将你的数字直接写成浮点数,所以这意味着至少有四种方法可以获得正确的结果。
综上所述,您需要在Python2中使用以下四种方式之一:
- 1//other_number
- 1.0/other_number
- float(1/other_number)
- 在您的脚本前面使用带有 from __future__ import division
语句的普通“/”。
EDIT3:事实证明,OP 正在使用 Python3
import numpy as np
import random
diam = 1.5
p = 1
a = 10
d = 0.2
h = 0.15
lx = list(np.arange(-diam/2, diam/2 + 0.05, 0.05))
# Are You aware that casting numpy array to list You loose precision?
# compare:
# print(np.arange(-diam/2, diam/2 + 0.05, 0.05))
# print(list(np.arange(-diam/2, diam/2 + 0.05, 0.05)))
# constants
x1 = 1/(2*p)
tan = np.tan(a)
cond1 = -d/2 - h/np.tan(np.pi/2 - a)
cond2 = d/2
disp1 = "abs( f - x1 * k**2 ) * tan = {} \n(f was {}, last k was {})\n"
print("cond1 = {}".format(cond1))
print("cond2 = {}".format(cond2))
print("")
for loop in range(1):
f = random.uniform(0, p/2)
a1 = [k for k in lx if k + abs(f - x1 * k**2 ) * tan < cond1]
a2 = [k for k in lx if k + abs(f - x1 * k**2 ) * tan > cond2]
a = a1 + a2
b = [k + abs(f - x1 * k**2 ) * tan for k in lx]
last = abs(f - x1 * lx[-1]**2) * tan
print(disp1.format(last, f, lx[-1]))
print("a1=", a1, "\n")
print("a2=", a2, "\n")
print("lx=", lx, "\n")
print("a=", a, "\n")
print("b=", b, "\n")
# we have:
# lx with uniform distribution from 0.75 to 0.75 with step 0.05
# a1 which will have values from lx up to condition
# a2 will have rest of lx's values
# effectively a which is sum of a1 and a2 should be equal lx
# in rare cases where condition is equal to cond1 or 2, should not.
# and we have b where i suppose You were testing what Your condition
# does.
# So looking at lx[i] and a[i], while checking b[i] versus cond1 or cond2
# We could see what You intended.
输出(Python3):
cond1 = -0.197254124118863
cond2 = 0.1
abs( f - x1 * k**2 ) * tan = 0.0266300386512908
(f was 0.32232286795171455, last k was 0.7500000000000013)
a1= [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969]
a2= [-0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
lx= [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, -0.14999999999999947, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
a= [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
b= [-0.72336996134870857, -0.64986688135331661, -0.57798470342657238, -0.50772342756847588, -0.43908305377902707, -0.37206358205822598, -0.30666501240607263, -0.24288734482256696, -0.18073057930770905, -0.12019471586149885, -0.061279754483936383, -0.0039856951750216196, 0.05168746206524541, 0.10573971723686473, 0.15817107033983635, 0.20898152137416023, 0.25817107033983638, 0.30573971723686488, 0.35168746206524559, 0.39601430482497857, 0.43872024551606381, 0.47980528413850143, 0.51926942069229121, 0.55711265517743336, 0.59333498759392778, 0.62793641794177446, 0.66091694622097341, 0.69227657243152474, 0.72201529657342822, 0.75013311864668408, 0.7766300386512921]
我假设您知道 f
每次在此 for 循环中获取随机值,并且您实际上为不同的 f 创建数组 a 和 b 50 次,但只有最后一个是可访问的,因为您不存储它们。也许您想将元素附加到此范围内的列表?
Ad rem,根据您正在尝试做的事情来判断,归结为从 lx 中选择满足 cond1 < item < cond2 的项目,而对于如何做到这一点可能有多种更好的解决方案,让我们检查一下您的。
如果我们观察上面的 b[i] where i=0...n 我们看到在这个例子中 8 个第一个值满足第一个条件,(小于 -0.197...),所以列表 a1 是填充了 lx 的前八个数字。
接下来,来自 b[i] 的五个连续数字不符合任何条件,并且 a2 从索引 14 开始(如果从 0 开始计数,则为 13),所以一切都是正确的。 a 由来自 lx 的前 8 个项目填充,下一个来自从索引 13 开始的剩余项目。
即使在您使用 -0.12 的第一个示例中...所有来自 lx 的满足条件的数字都在 L.
我对列表推导有疑问。
import numpy
import random
diam=1.5
p=1
a=10
d=0.2
h=0.15
lx = list(numpy.arange(-diam/2,diam/2+0.05,0.05))
for loop in range(50):
f=random.uniform(0,p/2)
l = [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)<-d/2-h/np.tan(np.pi/2-a))] + [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a))>(d/2)]
但是当我查看结果时,似乎有一些错误。
对于 a=10
,对于 f
的最后一个值,
l = [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
然后如果我输入这个
[k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a) for k in list(np.arange(-diam/2,diam/2+0.05,0.05))]
我明白了
[-0.72926117723233752, -0.68565303028301938, -0.64160406588192997, -0.59711428402906952, -0.54781631527556185, -0.49318773203196459, -0.43899996624013848, -0.38525301790008354, -0.33194688701179981, -0.27908157357528718, -0.22665707759054576, -0.17467339905757551, -0.12313053797637638, -0.072028494346948441, -0.021367268169291655, 0.028853140556593968, 0.07863273183070843, 0.12797150565305171, 0.17686946202362386, 0.22532660094242482, 0.27334292240945463, 0.32091842642471324, 0.36805311298820076, 0.41474698209991706, 0.4610000337598622, 0.50681226796803625, 0.55218368472443902, 0.60288571597093177, 0.65839593411807129, 0.71434696971698208, 0.77073882276766403]
(我将其称为 mylist
)
我知道 -d/2-h/np.tan(np.pi/2-a) = -0.12644904710626975
和 d/2 = 0.1
然后,如果我查看 mylist
中的值,我会注意到前 12 个值不如 -0.12644904710626975
。
但是,在l
中,k
只有11个连续值(-0.15
也应该在l
中)。
我不明白为什么 -0.15
不在列表 l
中。
编辑:这是另一个示例
l = [-0.75, -0.7 , -0.65, -0.6 , -0.55, -0.5 , -0.45, -0.4 , -0.35,
-0.3 , -0.25, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 ,
0.45, 0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75]
mylist = [-0.73693754824886837, -0.68054569519818642, -0.62459465959927574, -0.5690844414521361, -0.51401504075676774, -0.45938645751317042, -0.40519869172134437, -0.35145174338128943, -0.29814561249300564, -0.24528029905649307, -0.19285580307175165, -0.14087212453878137, -0.089329263457582256, -0.038227219828154309, 0.012434006349502473, 0.06265441507538809, 0.11243400634950255, 0.16177278017184585, 0.21067073654241797, 0.25912787546121896, 0.30714419692824874, 0.35471970094350741, 0.40185438750699487, 0.44854825661871117, 0.49480130827865632, 0.54061354248683036, 0.58598495924323313, 0.63091555854786485, 0.67540534040072531, 0.7194543048018146, 0.76306245175113285]
mylist
的前12个值不如-0.12644904710626975
,但在l
中,只有11个连续值:-0.2
应该在[=21中=] 也是。
(验证: k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)
for k = -0.2
, 我们得到 -0.1408721245387819
不如 -0.12644904710626975
)
好吧,我检查的方式 - 看起来不错。你能提供不同的样本吗,我很乐意研究它。
import numpy as np
import random
diam = 1.5
p = 1
a = 10
d = 0.2
h = 0.15
lx = list(np.arange(-diam/2, diam/2 + 0.05, 0.05))
for loop in range(50):
f = random.uniform(0, p/2)
a1 = [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)<-d/2-h/np.tan(np.pi/2-a))] + [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a))>(d/2)]
a2 = [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)<-d/2-h/np.tan(np.pi/2-a))]
a3 = [k for k in lx if (k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a))>(d/2)]
a4 = [k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a) for k in list(np.arange(-diam/2,diam/2+0.05,0.05))]
print "lx", lx
print "a1", a1
print "a2", a2
print "a3", a3
print "a4", a4
# however...
print -d/2-h/np.tan(np.pi/2-a)
print k+(abs(f-(1/(2*p))*(k**2)))*np.tan(a)
print -0.19999999999999951 < -d/2-h/np.tan(np.pi/2-a)
print -0.14999999999999947 < -d/2-h/np.tan(np.pi/2-a)
# EDIT2 => addition, see explanation below.
print "EDIT 2 results step by step equation from comment below answer"
print "==========================================="
x1 = (1/(2*p))
print "x1 = ", x1
x2 = (0.05**2)
print "x2 = ", x2
print "abs(f-x1*x2) = ", abs(f-x1*x2)
print "np.tan(a) = ", np.tan(a)
print "abs(f-x1*x2) * np.tan(a) = ", abs(f-x1*x2) * np.tan(a)
print "abs(f-x1*x2) * np.tan(a) + 0.05 = ", 0.05 + abs(f-x1*x2) * np.tan(a)
print "==========================================="
输出
lx [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, -0.14999999999999947, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
a1 [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
a2 [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951]
a3 [0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
a4 [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, -0.14999999999999947, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
-0.197254124119
0.75
True
False
EDIT 2 results step by step equation from comment below answer
===========================================
x1 = 0
x2 = 0.0025
abs(f-x1*x2) = 0.0
np.tan(a) = 0.648360827459
abs(f-x1*x2) * np.tan(a) = 0.0
abs(f-x1*x2) * np.tan(a) + 0.05 = 0.05
===========================================
编辑,回应@Jack 的评论和post 编辑。
@Jack - 您在编辑中提供的示例仍然不清楚。但首先:a4 和 lx 值相同,请随意将此 post 复制到新脚本并自行测试。您确定 post 中的值正确吗?您指的是 0.12644904710626975,而我的示例明确指出:
-d/2-h/np.tan(np.pi/2-a) != -0.12644904710626975
-d/2-h/np.tan(np.pi/2-a) == -0.197254124119
也许您在程序的其他地方更改了 d/h/a。
你如何分配一些变量来代替像这样的长结构,它们很难阅读并且可能会导致错误。
我也不知道,您在编辑中用来生成值的参数是什么。
也许你可以写下你想要实现的目标,这样解决问题可能更容易。
编辑 2:
在您下面的评论中您写道:
a4 and lx are not the same, I just tried and I get lx = [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, (...)
whereas a4 = [-0.67386015491279805, -0.64736323490818992, -0.61924541283493406, -0.58950668869303047, -0.54185293751752051, -0.47483346579671942, (...)
The value seems to be -0.197254124119. Sorry, I probably mixed different values... Anyway, for d/2 = 0.1, you can see that the last 15 values of mylist in the EDIT example are superior to 0.1, but there are only 14 consecutive values for the last values of l
And 0.05+(abs(f-(1/(2*p))*(0.05**2)))*np.tan(a) = 0.15540118660134225 > 0.1. So why 0.05 isn't in the list l ?
你有没有复制上面的脚本并在你的电脑上试过,和上面写的一模一样?我在上面添加了另外几行,以“==========”开头和结尾,以演示我从计算中获得的逐步结果。请注意,diam、p、a、d、h 在任何地方都没有改变。下面我写了原因 - 为什么会发生这种情况以及您可以做些什么来避免这种行为。
摘要
现在我们真正看到发生了什么。请观察 x1 得到了什么值。我们有 x1 == 0 而我们(作为人类)清楚地看到 1/(2*1) 是 0.5。
您可能知道为什么会发生这种情况,但如果您不知道,计算机就不会算作我们 - 它在 Python2 中称为整数除法(在 Python3、2/2 = 1.0
中已更改) ,所以在 Python2 "normal" 除法运算符“/”除以整数,而“//”除以浮点数。其他一些方法也是可能的,像这样 float(1)/2 == 0.5
或使用语句 from __future__ import division
将其中一个操作数转换为除法之前的浮点数。我很确定你也可以像这样 1.0/2 == 0.5
将你的数字直接写成浮点数,所以这意味着至少有四种方法可以获得正确的结果。
综上所述,您需要在Python2中使用以下四种方式之一:
- 1//other_number
- 1.0/other_number
- float(1/other_number)
- 在您的脚本前面使用带有 from __future__ import division
语句的普通“/”。
EDIT3:事实证明,OP 正在使用 Python3
import numpy as np
import random
diam = 1.5
p = 1
a = 10
d = 0.2
h = 0.15
lx = list(np.arange(-diam/2, diam/2 + 0.05, 0.05))
# Are You aware that casting numpy array to list You loose precision?
# compare:
# print(np.arange(-diam/2, diam/2 + 0.05, 0.05))
# print(list(np.arange(-diam/2, diam/2 + 0.05, 0.05)))
# constants
x1 = 1/(2*p)
tan = np.tan(a)
cond1 = -d/2 - h/np.tan(np.pi/2 - a)
cond2 = d/2
disp1 = "abs( f - x1 * k**2 ) * tan = {} \n(f was {}, last k was {})\n"
print("cond1 = {}".format(cond1))
print("cond2 = {}".format(cond2))
print("")
for loop in range(1):
f = random.uniform(0, p/2)
a1 = [k for k in lx if k + abs(f - x1 * k**2 ) * tan < cond1]
a2 = [k for k in lx if k + abs(f - x1 * k**2 ) * tan > cond2]
a = a1 + a2
b = [k + abs(f - x1 * k**2 ) * tan for k in lx]
last = abs(f - x1 * lx[-1]**2) * tan
print(disp1.format(last, f, lx[-1]))
print("a1=", a1, "\n")
print("a2=", a2, "\n")
print("lx=", lx, "\n")
print("a=", a, "\n")
print("b=", b, "\n")
# we have:
# lx with uniform distribution from 0.75 to 0.75 with step 0.05
# a1 which will have values from lx up to condition
# a2 will have rest of lx's values
# effectively a which is sum of a1 and a2 should be equal lx
# in rare cases where condition is equal to cond1 or 2, should not.
# and we have b where i suppose You were testing what Your condition
# does.
# So looking at lx[i] and a[i], while checking b[i] versus cond1 or cond2
# We could see what You intended.
输出(Python3):
cond1 = -0.197254124118863
cond2 = 0.1
abs( f - x1 * k**2 ) * tan = 0.0266300386512908
(f was 0.32232286795171455, last k was 0.7500000000000013)
a1= [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969]
a2= [-0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
lx= [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.34999999999999964, -0.2999999999999996, -0.24999999999999956, -0.19999999999999951, -0.14999999999999947, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
a= [-0.75, -0.69999999999999996, -0.64999999999999991, -0.59999999999999987, -0.54999999999999982, -0.49999999999999978, -0.44999999999999973, -0.39999999999999969, -0.099999999999999423, -0.049999999999999378, 6.6613381477509392e-16, 0.050000000000000711, 0.10000000000000075, 0.1500000000000008, 0.20000000000000084, 0.25000000000000089, 0.30000000000000093, 0.35000000000000098, 0.40000000000000102, 0.45000000000000107, 0.50000000000000111, 0.55000000000000115, 0.6000000000000012, 0.65000000000000124, 0.70000000000000129, 0.75000000000000133]
b= [-0.72336996134870857, -0.64986688135331661, -0.57798470342657238, -0.50772342756847588, -0.43908305377902707, -0.37206358205822598, -0.30666501240607263, -0.24288734482256696, -0.18073057930770905, -0.12019471586149885, -0.061279754483936383, -0.0039856951750216196, 0.05168746206524541, 0.10573971723686473, 0.15817107033983635, 0.20898152137416023, 0.25817107033983638, 0.30573971723686488, 0.35168746206524559, 0.39601430482497857, 0.43872024551606381, 0.47980528413850143, 0.51926942069229121, 0.55711265517743336, 0.59333498759392778, 0.62793641794177446, 0.66091694622097341, 0.69227657243152474, 0.72201529657342822, 0.75013311864668408, 0.7766300386512921]
我假设您知道 f
每次在此 for 循环中获取随机值,并且您实际上为不同的 f 创建数组 a 和 b 50 次,但只有最后一个是可访问的,因为您不存储它们。也许您想将元素附加到此范围内的列表?
Ad rem,根据您正在尝试做的事情来判断,归结为从 lx 中选择满足 cond1 < item < cond2 的项目,而对于如何做到这一点可能有多种更好的解决方案,让我们检查一下您的。
如果我们观察上面的 b[i] where i=0...n 我们看到在这个例子中 8 个第一个值满足第一个条件,(小于 -0.197...),所以列表 a1 是填充了 lx 的前八个数字。
接下来,来自 b[i] 的五个连续数字不符合任何条件,并且 a2 从索引 14 开始(如果从 0 开始计数,则为 13),所以一切都是正确的。 a 由来自 lx 的前 8 个项目填充,下一个来自从索引 13 开始的剩余项目。
即使在您使用 -0.12 的第一个示例中...所有来自 lx 的满足条件的数字都在 L.