我有一个看起来像
的对象scrollTo
我想分成以下格式
object = [{'number':'230/600','blah':'hi'},{'number':'240/620','blah':'hey'}]
有关如何根据[{'first':'230','second':'600', 'blah':'hi'},{'first':'240','second':'620','blah':'hey'}]
之前和之后的内容进行拆分的任何提示?
答案 0 :(得分:2)
假设格式已修复,您可以这样做:
>>> object = [{'number':'230/600'},{'number':'240/620'}]
>>> for value in object:
... numbers = value['number'].split('/')
... new_object.append({'numbera': numbers[0], 'numberb': numbers[1]})
...
>>> new_object
[{'numberb': '600', 'numbera': '230'}, {'numberb': '620', 'numbera': '240'}]
>>>
编辑:这是在编辑问题之前。迈克的回答对于编辑过的问题非常有用。
答案 1 :(得分:1)
这有效:
res = []
for item in L:
d = {}
for key, value in item.items():
split_value = value.split('/')
if len(split_value) > 1:
for entry, name in zip(split_value, ['first', 'second']):
d[name] = entry
else:
d[key] = value
res.append(d)
现在:
>>> res
[{'blah': 'hi', 'first': '230', 'second': '600'},
{'blah': 'hey', 'first': '240', 'second': '620'}]