如何创建嵌套循环

时间:2017-03-03 16:19:18

标签: python loops for-loop ipython nested-loops

我有一个结果清单

For user in users:
   print dct(user)

,结果是

user4 -> [diving]; user2->[swimming]; user6->[diving,swimming,running]; user8 ->[diving,swimming,skating]

我正在尝试创建一个嵌套循环来比较每个用户与其他用户(例如,将user4与user2,user6,user8进行比较,然后将user2与user6和user8进行比较)

1 个答案:

答案 0 :(得分:0)

# loop through each user in the users list
# use enumerate() so we also get the user's position in the list
for position,user1 in enumerate(users):
    # loop through each user in the remainder of the users list
    for user2 in users[position + 1:]
        compare_users(user1, user2)