初学者问题:将单元素元组转换为列表 [英] Beginner question: Converting Single-Element tuples to list
问题描述
大家好,
我想将一个元组转换为一个列表,我期待这种行为:
list((' 'abc'',''def'')) - > [''abc'',''def'']
list(('''abc'')) - > [''abc'']
但Python给了我这种行为:
list(('''abc'',''def '')) - > [''abc'',''def'']
list(('''abc'')) - > ['''',''b'',''c'']
如何让Python像前一种情况一样工作?
<非常感谢
非常感谢!
David
('' 'abc'')不是一个元组 - 这是使用()'作为
表达式分组*和*作为元组分隔符的不幸结果。要制作(''abc'')一个
元组,你必须添加一个额外的逗号,如'('abc'')。
< blockquote class =post_quotes>list((''abc'',))
[''abc '']
- Paul
谢谢你的回复!一个新的东西学到了....
请允许我跟着另一个问题:
让我们说我有一个来自名为pyparsing的模块的结果:
Results1 = [''abc'',''def'']
Results2 = [''abc'' ]
它们属于ParseResults类型:
type(结果1)
< class''pyparsing.ParseResults''> type(Results2)
< class''pyparsing.ParseResults''>
我想要的将它们转换为Python列表。 list()将在
Results1上正常工作,但在Results2上,它将返回:
['''',''b'', ''c'']
因为''abc''是一个字符串。但是我希望它返回[''abc'']。
简而言之,我的问题是:如何对任意对象进行类型转换,
是否一个类似列表的对象或一个字符串,进入一个Python列表而没有使用Python将我的字符串分成字符?$ / b
非常感谢你的帮助! br />
David
ps我知道pyparsing有一个asList()方法,但我只是说我是
在我的情况下无法使用它。
>
vd********@gmail.com 写道:
感谢您的回复!一个新的东西学到了....
请允许我跟着另一个问题:
让我们说我有一个名为pyparsing的模块的结果:<结果1 = [''abc'',''def'']
结果2 = [''abc'']
它们属于ParseResults类型:
type(Results1)< class''pyparsing.ParseResults''> type(Results2)
< 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'') is not a tuple - this is an unfortunate result of using ()''s as
expression grouping *and* as tuple delimiters. To make (''abc'') a
tuple, you must add an extra comma, as (''abc'',).
list( (''abc'',) )
[''abc'']
-- Paul
Hi,
Thanks for your reply! A new thing learned....
Allow me to follow that up with another question:
Let''s say I have a result from a module called pyparsing:
Results1 = [''abc'', ''def'']
Results2 = [''abc'']
They are of the ParseResults type:
type(Results1) <class ''pyparsing.ParseResults''> type(Results2)
<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?
Thanks very much for your help!
David
p.s. I know pyparsing has an asList() method, but let''s just say I am
unable to use it in my circumstances.
vd********@gmail.com wrote:Hi,
Thanks for your reply! A new thing learned....
Allow me to follow that up with another question:
Let''s say I have a result from a module called pyparsing:
Results1 = [''abc'', ''def'']
Results2 = [''abc'']
They are of the ParseResults type:type(Results1) <class ''pyparsing.ParseResults''> type(Results2)
<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 emulates
a list, it should do so every time, not only if there is more than one
value in it.
Reinhold
这篇关于初学者问题:将单元素元组转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!