【发布时间】:2019-10-30 23:00:30
【问题描述】:
我正在运行一个 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)
如果我在终端中运行它而不是在 jupyter 实验室中运行它,这会起作用,什么可能起作用?
【问题讨论】:
-
我在 jupyter 中运行了你的代码,最后得到了 [({'alex', 'tom'}, 'good'), ({'alex', 'james'}, 'bad' ), ({'james', 'tom'}, 'nice')]。这不是你所期待的吗?也许发布你的细胞的样子。
标签: python jupyter-notebook jupyter-lab