拆分字符串并创建列

时间:2019-02-06 10:29:49

标签: python string

我有一个这样的字符串列表:

List["String"] =["I am xyz", "My name is xyz", "myself xyz", "people call me xyz"]

我想将字符串拆分成这样的两列:

List["list"] = ["I am", "My name is", "myself", "people call me"]
List["Name"] = ["xyz","xyz","xyz","xyz"]

1 个答案:

答案 0 :(得分:2)

这是您要去的地方吗?但这显然不是答案,因为您在输入中没有给我们极端的情况:)

>>> x = ["I am xyz", "My name is xyz", "myself xyz", "people call me xyz"]
>>> d = {}
>>> d['list'] = [' '.join(y.split()[:-1]) for y in x]
>>> d['name'] = [y.split()[-1] for y in x]
>>> d
{'list': ['I am', 'My name is', 'myself', 'people call me'], 'name': ['xyz', 'xyz', 'xyz', 'xyz']}