我正在让自己陷入嵌套的困境。操纵嵌套在词典中的标签列表(.lstrip())嵌套在字典列表中
我有一个看起来像这样的Python对象的列表:
notes = [
{'id':1,
'title':'title1',
'text':'bla1 bla1 bla1',
'tags':['tag1a', ' tag1b', ' tag1c']},
{'id':2,
'title':'title2',
'text':'bla2 bla2 bla2',
'tags':[' tag2a', ' tag2b', ' tag2c']},
{'id':3,
'title':'title3',
'text':'bla3 bla3 bla3',
'tags':[' tag3a', ' tag3b', ' tag3c']}]
等。
我想进入列表中的每个字典,并去掉左空格,并返回字典列表,其中唯一的区别是标签有其不必要的空白空白。
下面的代码是我正在使用的,但它是不正确的,我不知道我在做什么来得到我需要的结果。
notes_cleaned = []
for objs in notes:
for items in objs:
notes_cleaned.append({'text':n['text'], 'id':n['id'], 'tags':[z.lstrip(' ') for z in n['tags']], 'title':n['title']})
这给了我,我不能使用字符串下标,这是我理解错误,但我不知道怎么做是正确的。因为我知道,我必须每个字典遍历,如:
for objs in notes:
for items in objs:
print items, objs[items]
,但我很困惑,如何去重建词典,同时挖掘到标签列表专门的最后一部分。
我在这里错过了什么(知道我肯定错过了什么)。
谢谢,这是最明确的解释,所以我选择了这一个。 – roy