< class''pyparsing.ParseResults''> 我想将它们转换为Python列表。 list()在 Results1上工作正常,但在Results2上,它会返回: ['''',''b'',''c''] 因为''abc''是一个字符串。但是我希望它返回[''abc'']。 简而言之,我的问题是:如何对一个任意对象进行类型转换,是一个类似列表的对象还是一个字符串,进入Python列表,没有让Python将我的字符串分成字符? 这似乎是pyparsing中的一个小问题。如果ParseResults类模拟 a列表,它应该每次都这样做,不仅仅是因为其中有多个 值。 Reinhold Hello everyone,I want to convert a tuple to a list, and I expected this behavior:list((''abc'',''def'')) -> [''abc'',''def'']list((''abc'')) -> [''abc'']But Python gave me this behavior:list((''abc'',''def'')) -> [''abc'',''def'']list((''abc'')) -> [''a'',''b'',''c'']How do I do get Python to work like the in former case?Many thanks!David 解决方案[''abc'']-- Paul<class ''pyparsing.ParseResults''>I want to convert them into Python lists. list() will work fine onResults1, but on Results2, it will return:[''a'', ''b'', ''c'']Because ''abc'' is a string. But I want it to return [''abc''].In short, my question is: how do I typecast an arbitrary object,whether a list-like object or a string, into a Python list withouthaving Python split my strings into characters?Thanks very much for your help!Davidp.s. I know pyparsing has an asList() method, but let''s just say I amunable to use it in my circumstances. <class ''pyparsing.ParseResults''> I want to convert them into Python lists. list() will work fine on Results1, but on Results2, it will return: [''a'', ''b'', ''c''] Because ''abc'' is a string. But I want it to return [''abc'']. In short, my question is: how do I typecast an arbitrary object, whether a list-like object or a string, into a Python list without having Python split my strings into characters?This seems like a glitch in pyparsing. If a ParseResults class emulatesa list, it should do so every time, not only if there is more than onevalue in it.Reinhold 这篇关于初学者问题:将单元素元组转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!