当我使用元组时,'int'类型的参数是不可迭代的

时间:2019-06-11 12:35:17

标签: python

运行程序时出现此错误,我不知道为什么。错误发生在显示if prt in migration_p[j][0] and dst in migration_p[j][1]

的行上
migration_p = [(1, 3), (2, 4), (3, 3)]
link = {(1, 2): 200, (1, 3): 50, (2, 3): 100, (1, 4): 300, (2, 4): 100, (3, 4): 50}
source_servers = {1: [1, 2, 3], 2: [1, 2, 3]}
partition = {1: 200, 2: 200, 3: 500}


def time_qi(dst, prt):
    global size, bandwidth, min_time, source

    tqi_rsrc = []
    indexes = []
    for i in source_servers.keys():
        if (i, dst) in link.keys():
            bandwidth = link[i, dst]
        for j in range(len(migration_p)):
            if prt in migration_p[j][0] and dst in migration_p[j][1]:
                size = partition[prt]
                tqi_rsrc.append(bandwidth / size)
                indexes.append(i)

    min_time = min(tqi_rsrc)
    index = np.argmin(tqi_rsrc)
    source = indexes[index]
    # print(source)

    return min_time, source

print(time_qi(3, 1)[0])

1 个答案:

答案 0 :(得分:5)

prtmigration_p[j][0]都是整数。您必须使用==而不是in

if prt == migration_p[j][0]

in用于检查可迭代对象中是否存在值。如果要使用in,则必须执行以下操作:

 if prt in migration_p[j]

但是请注意,它现在也可以与元组中的其他值匹配。