这是我的列表
List = ['function = function1', 'string = string1', 'hello = hello1', 'new = new1', 'test = test1']
我需要将列表分为两个不同的列表,分别为“键”和“值”
List = ['function = function1', 'string = string1', 'hello = hello1', 'new = new1', 'test = test1']
密钥列表
KeyList = ['function', 'string', 'hello', 'new', 'test']
值列表
ValueList = ['function1', 'string1', 'hello1', 'new1', 'test1']
4条答案
按热度按时间txu3uszq1#
有不同的可能的方法。一个是蒂姆提出的方法,但如果你不熟悉你也可以这样做:
输出为:
xhv8bpkk2#
您可以简单地使用
split(" = ")
并将键值对列表解压缩为两个元组:这是基于
zip(*a_zipped_iterable)
作为解压缩函数工作的事实。fjnneemd3#
我们可以在这里使用
re.findall
:kqlmhetl4#