python 嵌套列表中提取元素
a=[[name:zhe], [address:jsiajaij], [gender:males],[name:jiij],[address:hdushusb],[gender:female],[name:shdjsh], [address:jdsijdiisij], [gender:males]]
提取列表中所有name的字符串,address的字符串,gender的字符串
谢谢
python 嵌套列表中提取元素
a=[[name:zhe], [address:jsiajaij], [gender:males],[name:jiij],[address:hdushusb],[gender:female],[name:shdjsh], [address:jdsijdiisij], [gender:males]]
提取列表中所有name的字符串,address的字符串,gender的字符串
谢谢
这么写:
a = [['name:zhe'], ['address: jsiajaij'], ['gender: males'], ['name: jiij'], ['address: hdushusb'],
['gender: female'], ['name: shdjsh'], ['address: jdsijdiisij'], ['gender: males']]
name = []
address = []
gender = []
for i in a:
t=str(i).strip('[').strip(']').strip('\'').split(':')
if t[0] == 'name':
name.append(t[1])
if t[0] == 'address':
address.append(t[1])
if t[0] == 'gender':
gender.append(t[1])
print(name)
print(address)
print(gender)