5点(A,B,C,D,E)を線で結びたいです。
Forループで作るのは簡単ですが、繰り返しリンケージ(AB=BA)が発生します。
それを防ぐためのアイデアはありますか?
import itertools as it
points = ('A', 'B', 'C', 'D', 'E')
for pt1, pt2 in it.combinations(points, 2):
print '{0}-{1}'.format(pt1, pt2)
版画
A-B
A-C
A-D
A-E
B-C
B-D
B-E
C-D
C-E
D-E
内側のループを変更して、残りのアイテムのみを繰り返すことができます。
points = ['A', 'B', 'C', 'D', 'E']
for i, x in enumerate(points):
for y in points[i + 1:]:
print x, y