【发布时间】:2016-06-01 11:26:21
【问题描述】:
数据文件是一个 Excel 列表,有 4 列:名称、位置、价值和价格。使用 pandas 导入的文件。
有 5 个不同的位置。
我正在尝试匹配所有不同的可能性,其中每个位置只能是一个 + 一个尚未被选中的随机玩家。
def teams(x):
positions = list(data.Position.unique())
indices = []
for position in positions:
indices.append((data.Position == position).nonzero()[0])
positions.append('Random')
indices.append(data.index)
for i in indices[0]:
for i2 in indices[1]:
for i3 in indices[2]:
for i4 in indices[3]:
for i5 in indices[4]:
for i6 in indices[5]:
if i6 != i:
if i6 != i2:
if i6 != i3:
if i6 != i4:
if i6 != i5:
print i, i2, i3, i4, i5, i6
teams(data)
我的问题是我的循环太慢了,我无法让 if 语句工作,所以我必须创建 5 个不同的 ifs,我认为这是一个非常愚蠢的解决方案。
我的问题是:如何让我的循环更快/更智能,以及如何修复我的 if 语句
【问题讨论】:
-
你可以为你的 if 语句做
if i6 not in [i,i2,i3,i4,i5] -
每个索引有多少个元素?
-
atm 大约有 10 个,预计最后一个大约有 50 个
标签: python performance loops nested-loops