基本的Python列表拆分

时间:2017-01-29 03:41:33

标签: python-3.x

我想知道如何分割以下字符串列表:

给出清单:

l = ["George Smith", "Mark Johnson", "Cordell Theodore", "Maria Satterfield", "Johnson Cadence"]

需要的结果:

l1 = ["George", "Mark", "Cordell", "Maria", "Johnson"]

l2 = ["Smith", "Johnson", "Theodore", "Satterfield", "Cadence"]

1 个答案:

答案 0 :(得分:0)

你走了。这个答案涉及使用列表理解(如果您还不熟悉语法,请查阅它):

l = ["George Smith", "Mark Johnson", "Cordell Theodore", "Maria Satterfield", "Johnson Cadence"]
l1 = [x.split()[0] for x in l]
print(l1)
l2 = [x.split()[1] for x in l]
print(l2)

## ['George', 'Mark', 'Cordell', 'Maria', 'Johnson']
## ['Smith', 'Johnson', 'Theodore', 'Satterfield', 'Cadence']