Here is my program:
import sys
d = sys.stdin.readlines()
print(*d)
d = [x.strip(' ') for x in d]
print(*d)
Here is what happens when I run it:
>>> import program12
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
# EOF
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
My program needs to accept per line, 2 Strings separated by white-space, followed (optionally) by a number. I want to separate these with no white-space so it would be:
['Austin', 'Houston', 400]
I then want to put these in a 'graph' so I would use something like:
flights = collections.defaultdict(dict)
Any help is appreciated!
EDIT: First answer is fixed! In reference to my previous question, I have added this code, and this generates an error: Now I have this:
import sys
d = sys.stdin.readlines()
print(*d)
d = [x.split() for x in d]
print(*d)
flights = {}
for each in d:
flights[each.split()[0]][each.split()[1]] = each.split()[2]
And when I run:
>>> import program12
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
['Austin', 'Houston', '400'] ['SanFrancisco', 'Fresno', '700'] ['Miami', 'Ames', '500']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/program12.py", line 8, in <module>
flights[each.split()[0]][each.split()[1]] = each.split()[2]
AttributeError: 'list' object has no attribute 'split'
EDIT 2: My program:
import sys
import collections
d = sys.stdin.readlines()
d = filter(None,d.split('\n'))
flights = {each.split()[0]:{each.split()[1]:''} for each in d}
for each in d:
sp = each.split();flights[sp[0]][sp[1]] = '' if len(sp) <= 2 else sp[2]
New Error:
>>> import program12
Austin Houston 400
SanFrancisco Fresno 700
Miami Ames 500
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/program12.py", line 4, in <module>
d = filter(None,d.split('\n'))
AttributeError: 'list' object has no attribute 'split'