我正在创建一个 .txt 文件,该文件中有四名士兵在一个团队中按功绩排序。士兵不给自己排名。
我有以下代码:
import itertools
# create a list of 4 soldiers
numSoldiers=4
soldiers = []
for i in range(1,numSoldiers+1):
soldiers.append('soldier'+str(i))
# create list of all permutations of 2 soldiers
perms = list(itertools.permutations(soldiers, 2))
# list of possible rankings (soldier does not rank himself/herself)
possRanks = list(range(1,len(soldiers)))
# create the edgelist .txt file with randomized rankings
open('peerRankingsEdgelist.txt', 'w').close() #delete contents of .txt file at the start
file = open('peerRankingsEdgelist.txt', 'w')
for i in perms:
file.write(i[0] + ', ' + i[1] + '\n')
file.close() #close file
在 .txt 文件中生成以下输出:
soldier1, soldier2
soldier1, soldier3
soldier1, soldier4
soldier2, soldier1
soldier2, soldier3
soldier2, soldier4
soldier3, soldier1
soldier3, soldier2
soldier3, soldier4
soldier4, soldier1
soldier4, soldier2
soldier4, soldier3
对于每组三行(以相同开头的行组soldier
),我想添加一个随机排名,该排名来自possRanks
,其中possRanks = [1, 2, 3]
。问题是我不能完全随机化它,因为那样排名可能会重复。例如,soldier1
可能同时排名soldier2
和soldier3
排名为1
,这是我无法拥有的。
正确的示例输出如下:
soldier1, soldier2, 3
soldier1, soldier3, 1
soldier1, soldier4, 2
soldier2, soldier1, 1
soldier2, soldier3, 2
soldier2, soldier4, 3
soldier3, soldier1, 2
soldier3, soldier2, 1
soldier3, soldier4, 3
soldier4, soldier1, 1
soldier4, soldier2, 2
soldier4, soldier3, 3
在这里,用 的值、用 的值和用 的值进行soldier1
排名。soldier2
3
soldier3
1
soldier4
2