私はほぼ正確にこの問題を解決するために持っていたし、何もなかった場合は正の根を見つけ、ValueErrorを送出する機能を書いた:
def find_positive_root(a):
for b in a:
if type(b) == complex or b<0:
continue
return b
raise ValueError('No positive root in {}'.format(a))
このため、専用の機能を書くことの利点は、あなたということですそれをテストすることができます:
In [6]: find_positive_root([-4.3, 2.3j, 3, (-4+8j)])
Out[6]: 3
In [7]: find_positive_root([1., -4j, -2.3, 4j])
Out[7]: 1.0
In [8]: find_positive_root([-1., -4j, -2.3, 4j])
...:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-138-a4ed7675f551> in <module>()
----> 1 find_positive_root([-1., -4j, -2.3, 4j])
<ipython-input-132-9cd37f48e3bb> in find_positive_root(a)
4 continue
5 return b
----> 6 raise ValueError('No positive root in {}'.format(a))
ValueError: No positive root in [-1.0, (-0-4j), -2.3, 4j]
出典
2017-05-17 14:47:29
xnx
あなたの関数(i)、それは何を返しますか...そしてbは何ですか? –
なぜ 'a'をif文に入れるのですか?それはリストです。 'b'はリスト項目のようにチェックする必要があります – j2ko
' 3'のような正のint型か、 '3.0'のような正の' float'型か '3 + 0jのような虚数部のない正の複合体'? –