を与えますx
変数が前の行で定義したx
と同じではなく、であるため、x
からfor x in table
。だからx
はtable
の次のキーです。そして、すべてのキーが1000より大きいので、作成されたイテレータは空ではありません。 x
あなたは空のイテレータから次の反復を取得していることを意味する、x
より大きくなることはありませんので、
next(x for x in table if x > 1000)
# similar to:
# next(iter((1249.99, 1749.99, 249.99, 2749.99, 3249.99, 3749.99, 4249.99)))
第二の例は、StopIteration
を上げました。
あなたのコードはこれと同等です:
next(x for x in table if x > x)
# similar to:
# next(iter(()))
は、次の点を考慮
def gen_a(table):
for x in table: # same as for x in table.keys()
if x > 1000:
yield x
def gen_b(table):
for x in table: # same as for x in table.keys()
if x > x: # will never happen
yield x
table ={1249.99: 36.30,
1749.99: 54.50,
2249.99: 72.70,
2749.99: 90.80,
3249.99: 109.00,
3749.99: 127.20,
4249.99: 145.30}
x = 1000 # note that x isn't in the same scope as the other x's
print(next(gen_a(table))) # result varies since dict are unordered, I got 4249.99
print(next(gen_b(table))) # raises a StopIteration
複製することはできません両方 'あなた' table'は、それが実証するために何かを持っているべきStopIteration' – AChampion
を上げます問題? – mgilson
'x> x'は決して真ではないので、イテレータは*常に*空です –