Python,将元组项拆分为单个内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18372952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python, split tuple items to single stuff
提问by dzordz
I have tuple in Python that looks like this:
我在 Python 中有一个看起来像这样的元组:
tuple = ('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook')
and I wanna split it out so I could get every item from tuple independent so I could do something like this:
我想把它分开,这样我就可以从独立的元组中获取每个项目,这样我就可以做这样的事情:
domain = "sparkbrowser.com"
level = 0
url = "http://facebook.com/sparkbrowser"
text = "Facebook"
or something similar to that, My need is to have every item separated. I tried with .split(",")
on tuple but I've gotten error which says that tuple doesn't have split option
或类似的东西,我需要将每个项目分开。我.split(",")
在元组上试过,但我收到错误,说元组没有拆分选项
Any help or advice is welcome
欢迎任何帮助或建议
采纳答案by Ignacio Vazquez-Abrams
Python can unpack sequences naturally.
Python 可以自然地解压缩序列。
domain, level, url, text = ('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook')
回答by Joyfulgrind
>>> domain, level, url, text = ('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook')
>>> domain
'sparkbrowser.com'
>>> level
0
>>> url
'http://facebook.com/sparkbrowser'
>>> text
'Facebook'
回答by John La Rooy
Best not to use tuple
as a variable name.
最好不要tuple
用作变量名。
You might use split(',')
if you had a string like 'sparkbrowser.com,0,http://facebook.com/sparkbrowser,Facebook'
, that you needed to convert to a list. However you already have a tuple, so there is no need here.
split(',')
如果您有一个像 那样的字符串'sparkbrowser.com,0,http://facebook.com/sparkbrowser,Facebook'
,您可能会使用它,您需要将其转换为列表。但是你已经有了一个元组,所以这里没有必要。
If you know you have exactly the right number of components, you can unpack it directly
如果你知道你有正确数量的组件,你可以直接打开它
the_tuple = ('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook')
domain, level, url, text = the_tuple
Python3 has powerful unpacking syntax. To get just the domain
and the text
you could use
Python3 具有强大的解包语法。为了得到公正的domain
和text
可以使用
domain, *rest, text = the_tuple
rest
will contain [0, 'http://facebook.com/sparkbrowser']
rest
将包含 [0, 'http://facebook.com/sparkbrowser']
回答by Keyur Potdar
An alternative for this, is to use collections.namedtuple
. It makes accessing the elements of tuples easier.
对此的替代方法是使用collections.namedtuple
. 它使访问元组的元素更容易。
Demo:
演示:
>>> from collections import namedtuple
>>> Website = namedtuple('Website', 'domain level url text')
>>> site1 = Website('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook')
>>> site2 = Website('foo.com', 4, 'http://bar.com/sparkbrowser', 'Bar')
>>> site1
Website(domain='sparkbrowser.com', level=0, url='http://facebook.com/sparkbrowser', text='Facebook')
>>> site2
Website(domain='foo.com', level=4, url='http://bar.com/sparkbrowser', text='Bar')
>>> site1.domain
'sparkbrowser.com'
>>> site1.url
'http://facebook.com/sparkbrowser'
>>> site2.level
4