Python,将tuple项拆分为单个东西

时间:2021-08-19 00:33:39

I have tuple in Python that looks like this:

Python中的tuple是这样的:

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

或者类似的东西,我需要把每一件东西分开。我在tuple上尝试了。split(",")但是我得到了一个错误,说tuple没有split选项

Any help or advice is welcome

欢迎任何帮助或建议

4 个解决方案

#1


21  

Python can unpack sequences naturally.

Python可以自然地解压序列。

domain, level, url, text = ('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook')

#2


3  

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.

如果你有一个字符串,如“sparkbrowser.com,0,http://facebook.com/sparkbrowser,Facebook”,你需要转换成一个列表,你可以使用split(',',')。但是您已经有了一个tuple,所以这里不需要。

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, *rest, text = the_tuple

rest will contain [0, 'http://facebook.com/sparkbrowser']

rest将包含[0,'http://facebook.com/sparkbrowser']

#3


2  

>>> domain, level, url, text = ('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook')
>>> domain
'sparkbrowser.com'
>>> level
0
>>> url
'http://facebook.com/sparkbrowser'
>>> text
'Facebook'

#4


0  

An alternative for this, is to use collections.namedtuple. It makes accessing the elements of tuples easier.

另一种方法是使用collection .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

#1


21  

Python can unpack sequences naturally.

Python可以自然地解压序列。

domain, level, url, text = ('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook')

#2


3  

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.

如果你有一个字符串,如“sparkbrowser.com,0,http://facebook.com/sparkbrowser,Facebook”,你需要转换成一个列表,你可以使用split(',',')。但是您已经有了一个tuple,所以这里不需要。

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, *rest, text = the_tuple

rest will contain [0, 'http://facebook.com/sparkbrowser']

rest将包含[0,'http://facebook.com/sparkbrowser']

#3


2  

>>> domain, level, url, text = ('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook')
>>> domain
'sparkbrowser.com'
>>> level
0
>>> url
'http://facebook.com/sparkbrowser'
>>> text
'Facebook'

#4


0  

An alternative for this, is to use collections.namedtuple. It makes accessing the elements of tuples easier.

另一种方法是使用collection .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