我有一个包含这样的项目的文件: -
0 -> 205,3
1 -> 0,110,18,49,8
我需要转换为字典: -
{0:[205,3],
1:[0,110,18,49,8]}
我试过了: -
def read_input(filename):
f = open(filename, 'r')
g = defaultdict(list)
for line in f:
line = line.strip()
line = line.split(' -> ')
g[int(line[0])].append(line[1])
return g
我的结果: -
defaultdict(list,
{0: ['205,3'],
1: ['0,110,18,49,8']})
我也尝试过: -
def read_input(filename):
f = open(filename, 'r')
g = defaultdict(list)
for line in f:
line = line.strip()
line = line.split(' -> ')
g[int(line[0])].append(int(x) for x in line[1].split(','))
return g
结果: -
defaultdict(list,
{0: [<generator object <genexpr> at 0x110e01410>],
1: [<generator object <genexpr> at 0x110e01460>]})