Python jupyter实验室单元中循环内的输入

Python jupyter实验室单元中循环内的输入,python,jupyter-notebook,jupyter-lab,Python,Jupyter Notebook,Jupyter Lab,我正在运行一个jupyter实验室笔记本 我有一个名称列表,我希望能够输入列表中每个项目之间关系的一些信息 我的想法通常是遍历for循环,但我认为在jupyter中不可能遍历输入 names = ['alex','tom','james'] relationships = [] for i in names: for j in names: if j==i: continue skip = False for r i

我正在运行一个jupyter实验室笔记本 我有一个名称列表,我希望能够输入列表中每个项目之间关系的一些信息

我的想法通常是遍历for循环,但我认为在jupyter中不可能遍历输入

names = ['alex','tom','james']
relationships = []
for i in names:
    for j in names:
        if j==i:
            continue
        skip = False
        for r in relationships:
            if r[0] == {i,j}:
                skip = True
                break
        if skip == True:
            # print(f'skipping {i,j}')
            continue
        strength = input(f'what is the relationship between {i} and {j}?')
        relationships.append(({i,j},strength))

print(relationships)


如果我在terminal中运行它,而不是在jupyter lab中运行它,这是可行的,那么什么可能有效呢?

您可以使用
itertools.permutations()
进一步简化您的代码

例如:

导入itertools
姓名=['alex'、'tom'、'james']
unique=set()
置换=集合(itertools.permutations(名称,2))
对于排列中的配对:
如果配对不唯一且配对[:-1]不唯一:
唯一。添加(对)
关系=[]
对于具有唯一性的配对:
强度=输入(f'在{pair[0]}和{pair[1]}之间的关系是什么?')
关系。追加((对,强度))
打印(关系)
控制台输出:

what is the relationship between alex and james?12
what is the relationship between alex and tom?5
what is the relationship between james and tom?6
[(('alex', 'james'), '12'), (('alex', 'tom'), '5'), (('james', 'tom'), '6')]

但是,即使您的代码在jupyter笔记本中也似乎运行良好:

您可以尝试重新启动python内核,即在菜单中转到Kernal->Restart并运行all


相关问题:


我在jupyter中运行了你的代码,最后得到了[({'alex','tom','good'),({'alex','james','bad'),({'james','tom','nice')。这不是你所期望的吗?也许张贴你的细胞的外观。