从sets.Set获取一些元素 [英] Getting some element from sets.Set

查看:105
本文介绍了从sets.Set获取一些元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法索引设置对象。没关系。

但是,如果我想从Set中找到一些元素怎么办。


from sets import

s =设置(范围(12)


如果我弹出,该特定元素被删除。

我不想删除元素,但得到一些元素
来自套装的



s.some_element()#不可用


有没有办法要做到这一点。我这样做:

对于x中的
:break


现在x是/ some_element /来自s。


-

Suresh

解决方案

jm ******* @ no.spam.gmail.com 写道:


索引设置对象是不可能的。没关系。

但是,如果我想从Set中找到一些元素怎么办。


from sets import Set

s = Set(range(12)


如果我弹出,那个特定的元素ge删除了。

我不想删除元素,但从Set中获取一些元素




s。 some_element()#不可用


有没有办法做到这一点。我是这样做的:

$ s $ b for x:s


现在x是/ some_element /来自s。



然后一个集合可能不是合适的容器。你的用例是什么?


彼得


2007年5月3日星期四23:08:33 -0700, jm ******* @ no.spam.gmail.com 写道:
< blockquote class =post_quotes>
索引设置对象是不可能的。没关系。

但是,如果我想从Set中找到一些元素怎么办。


from sets import

s =设置(范围(12)


如果我弹出,该特定元素被删除。

我不想删除元素,但得到一些元素

来自Set。


s.some_element()#不可用



看看帮助(sets.Set),似乎没有直接的方法可以要求

a单个元素,除了pop。所以你可以弹出一个元素,

然后将其添加回来:


some_element = s.pop()

s.add(some_element)


另一个解决方案是提取所有元素,然后选择一个:


some_element = list(s)[0]

-

Steven D''Aprano


我不想删除元素,但得到一些元素


来自Set。


。 。 。


有没有办法做到这一点。我是这样做的:

$ s $ b for x:s


现在x是/ some_element /来自s。



这是一种方法。另一个是写:

x = iter(s).next()


另一种方法:


x = s.pop()

s.add(x)

Raymond Hettinger


It is not possible to index set objects. That is OK.
But, what if I want to find some element from the Set.

from sets import Set
s = Set( range(12 )

if I do pop, that particular element gets removed.
I do not want to remove the element, but get some element
from the Set.

s.some_element() # Is not available

Is there a way to do this. I am doing it like this:

for x in s: break

Now x is /some_element/ from s.

-
Suresh

解决方案

jm*******@no.spam.gmail.com wrote:

It is not possible to index set objects. That is OK.
But, what if I want to find some element from the Set.

from sets import Set
s = Set( range(12 )

if I do pop, that particular element gets removed.
I do not want to remove the element, but get some element
from the Set.

s.some_element() # Is not available

Is there a way to do this. I am doing it like this:

for x in s: break

Now x is /some_element/ from s.

A set is probably not the appropriate container then. What is your use case?

Peter


On Thu, 03 May 2007 23:08:33 -0700, jm*******@no.spam.gmail.com wrote:

It is not possible to index set objects. That is OK.
But, what if I want to find some element from the Set.

from sets import Set
s = Set( range(12 )

if I do pop, that particular element gets removed.
I do not want to remove the element, but get some element
from the Set.

s.some_element() # Is not available

Looking at help(sets.Set), it seems that there is no direct way to ask for
a single element of a set, except with pop. So you can pop an element,
then add it back in:

some_element = s.pop()
s.add(some_element)

Another solution is to extract all the elements, then pick one:

some_element = list(s)[0]
--
Steven D''Aprano


I do not want to remove the element, but get some element

from the Set.

. . .

Is there a way to do this. I am doing it like this:

for x in s: break

Now x is /some_element/ from s.

That is one way to do it. Another is to write:
x = iter(s).next()

One more approach:

x = s.pop()
s.add(x)
Raymond Hettinger


这篇关于从sets.Set获取一些元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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