我有一个字符串
a = "sequence=1,2,3,4&counter=3,4,5,6"
如何将其转换成列表,即
sequence = [1,2,3,4]
counter = [3,4,5,6]
最佳答案
使用urlparse.parse_qs
(urllib.parse.parse_qs
在 Python 3.x 中)解析查询字符串:
>>> import urlparse
>>> a = "sequence=1,2,3,4&counter=3,4,5,6"
>>> {key: [int(x) for x in value[0].split(',')]
... for key, value in urlparse.parse_qs(a).items()}
{'counter': [3, 4, 5, 6], 'sequence': [1, 2, 3, 4]}
关于Python - 解析字符串并将其转换为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24971593/