下面是我的示例代码:
list1 = [{'name': 'foobar', 'parents': 'John Doe and Bartholomew Shoe'},
{'name': 'Wisteria Ravenclaw', 'parents': 'Douglas Lyphe and Jackson Pot'
}]
我需要将父级拆分为一个列表并删除'and'字符串。所以输出应该是这样的:
list1 = [{'name': 'foobar', 'parents': ['John Doe', 'Bartholomew Shoe'],
{'name': 'Wisteria Ravenclaw', 'parents': ['Douglal Lyphe', 'Jackson', 'Pot']
}]
请帮我弄清楚。你知道吗
for people in list1:
people['parents'] = people['parents'].split('and')
我不知道怎么移动那根绳子。你知道吗
扩展其他人所说的:您可能希望在正则表达式上拆分,以便
and
上拆分and
周围的空白。你知道吗像这样:
应该在循环中使用
people
,而不是迭代器本身。你知道吗当你打印
list1
时,你会得到:相关问题 更多 >
编程相关推荐