如何从一个简单的集合中获取一个项目? [英] How to get an item from a simple set?

查看:63
本文介绍了如何从一个简单的集合中获取一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个项目的集合。在该项目获得

的最佳方法是什么?使用pop()清空集合。以下是我的尝试。


Python 2.3.4(#1,2004年6月13日,11:21:03)

[GCC 3.3 .1(cygming special)]关于cygwin

输入help,copyright,credit等等。或许可证或更多信息。

from sets import
s = Set([''foo''])
s.copy()。pop()
''foo''[x for x in s] [0]
''foo''[0]



回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:unindexable object


-

Pete Forman -./\.-免责声明:这篇文章来自

WesternGeco -./\.-由我自己并不代表
pe ******* **@westerngeco.com -./\.-斯伦贝谢,贝克的意见
http://petef.port5.com -./\ .-休斯或他们的部门。

解决方案

Pete Forman写道:
我有一个包含一个项目的集合。什么是获得该项目的最佳方式?使用pop()清空集合。这是我尝试过的。




这是元组解包的用途:

s = set([''foo''])
item,= s
item
''foo''[item] = s
item



''foo''


这取决于你是否你更喜欢元组或列表语法。 =)


Steve




Pete>我有一个包含一个项目的集合。什么是最好的方式

Pete>得到那个项目?使用pop()清空集合。


你想枚举集合中的所有项目吗?如果是这样的话:


elt in s:

print elt


如果你只想抓一个任意的(虽然不是随机的)项目来自

套装,试试:


elt = iter(s).next()


请注意,重复此操作将始终返回相同的项目:

s
set ([''jkl'',''foo'',''abc'',''def'',''ghi''])iter(s).next()
''jkl'' iter(s).next()
''jkl''iter(s).next()
''jkl''iter(s).next()



''jkl''


Skip


Skip Montanaro< sk **@pobox.com>写道:

Pete>我有一个包含一个项目的集合。什么是最好的方式
Pete>得到那个项目?使用pop()清空该集。

如果您只想从
集中获取一个任意(但不是随机)项,请尝试:

elt = iter(s).next()




我实际上想把单个项目附加到一个字符串,Steven的

解决方案工作分配。


所以这看起来是我最好的选择。我可能会在我的代码中使用join而不是+ =


line =''bar''
line + = iter(s).next()
line



'' bar foo''


-

Pete Forman -./\.-免责声明:这篇文章来自

WesternGeco -./\.-由我自己并不代表
pe ********* @ westerngeco.com -./\.-斯伦贝谢,贝克的意见
http://petef.port5.com -./\.-休斯或他们的部门。


I have a set that contains one item. What is the best way of getting
at that item? Using pop() empties the set. Here is what I''ve tried.

Python 2.3.4 (#1, Jun 13 2004, 11:21:03)
[GCC 3.3.1 (cygming special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.

from sets import Set
s = Set([''foo''])
s.copy().pop() ''foo'' [x for x in s][0] ''foo'' s[0]


Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unindexable object

--
Pete Forman -./\.- Disclaimer: This post is originated
WesternGeco -./\.- by myself and does not represent
pe*********@westerngeco.com -./\.- opinion of Schlumberger, Baker
http://petef.port5.com -./\.- Hughes or their divisions.

解决方案

Pete Forman wrote:

I have a set that contains one item. What is the best way of getting
at that item? Using pop() empties the set. Here is what I''ve tried.



This is what tuple unpacking is for:

s = set([''foo''])
item, = s
item ''foo'' [item] = s
item


''foo''

It''s up to you whether you like the tuple or list syntax better. =)

Steve



Pete> I have a set that contains one item. What is the best way of
Pete> getting at that item? Using pop() empties the set.

Do you want to enumerate all the items in the set? If so:

for elt in s:
print elt

If you just want to grab one arbitrary (though not random) item from the
set, try:

elt = iter(s).next()

Note that repeating this operation will always return the same item:

s set([''jkl'', ''foo'', ''abc'', ''def'', ''ghi'']) iter(s).next() ''jkl'' iter(s).next() ''jkl'' iter(s).next() ''jkl'' iter(s).next()


''jkl''

Skip


Skip Montanaro <sk**@pobox.com> writes:

Pete> I have a set that contains one item. What is the best way of
Pete> getting at that item? Using pop() empties the set.

If you just want to grab one arbitrary (though not random) item from the
set, try:

elt = iter(s).next()



I actually wanted to append the single item to a string, Steven''s
solutions work for assignment.

So this looks like my best bet. I''ll probably use join instead of +=
in my code.

line = ''bar ''
line += iter(s).next()
line


''bar foo''

--
Pete Forman -./\.- Disclaimer: This post is originated
WesternGeco -./\.- by myself and does not represent
pe*********@westerngeco.com -./\.- opinion of Schlumberger, Baker
http://petef.port5.com -./\.- Hughes or their divisions.


这篇关于如何从一个简单的集合中获取一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆