Smart猫小萌
您需要为每行创建一个新的列表对象,然后替换原始列表值:def row_to_values(row): values = [] for col in row: if isinstance(col, unicode) and col != u'f': # split and convert all entries to float values += (float(v) for v in col.split(',')) else: values.append(col) return valuesfor value in data.values(): value[:] = [row_to_values(row) for row in value]该value[:] = ...分配告诉Python来代替包含的所有指标中的列表对象具有一组新的对象。由于每个value列表都是data词典中的外部列表,因此将所有子列表替换为扩展的行。演示部分样本数据:>>> data = {"technology1": [... [... 20, 0.02,... u'10.00,106.10,107.00,107.00,0.45',... u'24.00,-47.15,-49.50,-51.00,0.12',... u'11.00,0.35,0.00,0.00,0.92',... u'0.00',0.04,0.16, u'0.223196881092', u'f',0.02,... ],... [... 100, 0.02,... u'10.00,106.10,107.00,107.00,0.45',... u'24.00,-47.15,-49.50,-51.00,0.12',... u'11.00,0.35,0.00,0.00,0.92', u'0.00', 0.04,... 0.16, u'0.223196881092', u'f', 0.01... ]],... }>>> from pprint import pprint>>> pprint(data["technology1"][0])[20, 0.02, u'10.00,106.10,107.00,107.00,0.45', u'24.00,-47.15,-49.50,-51.00,0.12', u'11.00,0.35,0.00,0.00,0.92', u'0.00', 0.04, 0.16, u'0.223196881092', u'f', 0.02]>>> pprint(row_to_values(data["technology1"][0]))[20, 0.02, 10.0, 106.1, 107.0, 107.0, 0.45, 24.0, -47.15, -49.5, -51.0, 0.12, 11.0, 0.35, 0.0, 0.0, 0.92, 0.0, 0.04, 0.16, 0.223196881092, u'f', 0.02]因此,可以通过返回新列表对象的函数调用来扩展一行,使其包含字符串中的所有浮点值。使用该函数替换所有字典值中的所有行:>>> for value in data.values():... value[:] = [row_to_values(row) for row in value]...我们可以看到之前查看的第一行已更新:>>> pprint(data["technology1"][0])[20, 0.02, 10.0, 106.1, 107.0, 107.0, 0.45, 24.0, -47.15, -49.5, -51.0, 0.12, 11.0, 0.35, 0.0, 0.0, 0.92, 0.0, 0.04, 0.16, 0.223196881092, u'f', 0.02]字典的其余部分也是如此:>>> pprint(data){'technology1': [[20, 0.02, 10.0, 106.1, 107.0, 107.0, 0.45, 24.0, -47.15, -49.5, -51.0, 0.12, 11.0, 0.35, 0.0, 0.0, 0.92, 0.0, 0.04, 0.16, 0.223196881092, u'f', 0.02], [100, 0.02, 10.0, 106.1, 107.0, 107.0, 0.45, 24.0, -47.15, -49.5, -51.0, 0.12, 11.0, 0.35, 0.0, 0.0, 0.92, 0.0, 0.04, 0.16, 0.223196881092, u'f', 0.01]]}