Reputation: 787
I'm running a jupyter lab notebook I have a list of names and I want to be able to input some information for relationships between each item in the list.
My thought would normally be to iterate through a for loop but I don't think in jupyter it is possible to loop through inputs.
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)
This works if i run it in terminal but not in jupyter lab, what might work?
Upvotes: 1
Views: 1704
Reputation: 1839
You can further simplify your code using itertools.permutations()
.
Example:
import itertools
names = ['alex','tom','james']
unique = set()
permutations = set(itertools.permutations(names, 2))
for pair in permutations:
if pair not in unique and pair[::-1] not in unique:
unique.add(pair)
relationships = []
for pair in unique:
strength = input(f'what is the relationship between {pair[0]} and {pair[1]}?')
relationships.append((pair,strength))
print(relationships)
Console Output:
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')]
However, even your code seems to be working fine in jupyter notebook:
You can maybe try restarting the python kernel i.e. in the menu go to Kernal -> Restart and Run all.
Related question:
How to give jupyter cell standard input in python?
Upvotes: 1