0

これはばかげているかもしれません。私は今それを理解することはできません。

合計数(x)があります

(y)で均等に分割する必要があるのはどれですか

x が 10,000 で y が 10 の場合、

つまり、10,000 は 10 の間に吐き出されるということです。

始点の見つけ方

1 starts at 1 and ends 1,000
2 starts at 1,001 & ends 2,000
3 ..
4 ..
5 ..
4

1 に答える 1

7

これは実際には単純な計算にすぎません。

x = 10000
y = 10
print([(item, item+(x//y-1)) for item in range(1, x, x//y)])

私たちに与えます:

[(1, 1000), (1001, 2000), (2001, 3000), (3001, 4000), (4001, 5000), (5001, 6000), (6001, 7000), (7001, 8000), (8001, 9000), (9001, 10000)]

ここではrange()ビルトインリスト内包表記を使用します。

これは、ビルトインを使用しrange()てジェネレーター from 1to belowを構築し、xx除算 (整数除算なので、浮動小数点数は得られません) することで機能しyます。

次に、リスト内包表記を使用してこれらの値 ( 1, 1001, 2001, ..., 9001) を取得し、それらをタプルのペアに入れ、値に(x//y-1)(この場合は999) を追加して終了境界を取得します。

当然のことながら、たとえばこれをループで使用したい場合は、リスト内包表記よりも遅延評価されるようにジェネレーター式を使用した方がよいでしょう。例えば:

>>> for number, (start, end) in enumerate((item, item+(x//y-1)) for item in range(1, x, x//y)): 
...     print(number, "starts at", start, "and ends at", end)
... 
0 starts at 1 and ends at 1000
1 starts at 1001 and ends at 2000
2 starts at 2001 and ends at 3000
3 starts at 3001 and ends at 4000
4 starts at 4001 and ends at 5000
5 starts at 5001 and ends at 6000
6 starts at 6001 and ends at 7000
7 starts at 7001 and ends at 8000
8 starts at 8001 and ends at 9000
9 starts at 9001 and ends at 10000
于 2012-05-13T12:02:15.803 に答える