これは学校での練習用です。これも初めてですので少しお待ちください。私はグラフ 'fileName'と隣接番組を出力するプログラムを持っています。マトリックスはそのようなリストに配置されています。この隣接行列でPythonを使用してテキストファイルから特定の行を印刷する
[[0, 1, 1, 0], [1, 0, 1, 1], [1, 1, 0, 0], [0, 1, 0, 0]]
、それはお互いに対戦したチームがリストされている理論的なスポーツのマッチアップを示しています「1」といない人は、「記載されています0 'である。私は別のファイル 'fileName2'を持っています。このファイルには、テキストファイルの各行に1つのチーム名が含まれているチーム名がリストされています。私はお互いにゲームをしていないチームの名前を印刷することを目指しています。私のコードは、これまで(それはアーティファクトのデバッグをたくさん持っている、私が知っている、それは厄介だ)以下見つけることができます:
vertices = int(input('How many vertices are there in the graph?'))
fileName = input("What's the name of the file of graph to read?")
fileName2 = input("What's the name of the file of team names to read?")
f1 = open(fileName, 'r')
f2 = open(fileName2, 'r')
# Create adjacency matrix skeleton for storing later
adjMatrix = [ [0] * vertices for i in range(vertices) ]
# adjMatrix = [[0] * vertices] * vertices
# Looping through the file to obtain the connecting vertices
for line in f1:
line = line.replace('\n', '')
line = line.split(' ')
i = 0
for vert in line:
line[i] = int(vert)
i += 1
j = line[0]
k = line[1]
adjMatrix[j][k] = 1
adjMatrix[k][j] = 1
print(adjMatrix)
# Print out the teams who have yet to play against each other
for m in range(vertices):
# print(adjMatrix[m])
for n in range(vertices):
# print(adjMatrix[m][n])
if m != n and adjMatrix[m][n] == 0:
if m < n:
print(m,n)
t = 0
for team in f2:
if m == t:
print(team)
if n == t:
print(team)
t += 1
# Is this elif statement necessary as we sort through the teams?
elif m > n:
print(n,m)
# for team in f2:
# print(team)
f1.close()
f2.close()
このコードの問題は、これまで「F2でチームのために」ループがないことですそのコード行が 'if m < n'ステートメントから呼び出されると、再びループします。このため、まだ演奏されていない1つのマッチアップだけが印刷されますが、他のマッチアップは演奏されません。
あり、この(0-3及び2-3)から2まだツー・再生することがマッチアップすることになってますので、0-3のためではなく、2-3にリストされている名前を出力します。
はどのように知りたい: 1.ファイル「F2」をループのために問題を修正します。 2.私がこれまでにまとめたこのコードを改善してください。
ありがとうございます。
注:ファイルF2の上に1時間を繰り返すとき、私は再び開始するためにファイルを巻き戻すためにはPython 3.5.3
は私が始める前に置かれ、このコマンドであり、ループは再びt = 0と言う。 f2.seek(0); forループが再び始まる前に? –
はい、まさにそこにあります。 – user4624500