“Iteration” of sets

global x
global i
x.index(i) >= x.index(i+1)

I have a (set) i in x and wish to reference another thing in x

I also need i to have more in it than anything else

So far the rubbish I come up with to do this needs vacancy… I can’t complete this code because I don’t even know how it works… I’ve asked around and have a pretty good idea that this is what I’m going for.

Howdy Jack,

sorry, I do not get, what you want. Can you please give us a concrete example?

“another thing” and “than anything else” and alike are not very helpful in this sense :blush:

Cheers, Dominik

This looks like either:

  • you want to test the position of the value “i” in the set “x”, which
    doesn’t work because sets are unordered and also do not have a .index
    method (for the same reason)
  • you’re counting the number of things with value “i”

If the latter then you don’t want a set, you want a dict which maps
values of “i” to a count. Eg:

d = {1: 3, 5: 4}    # some initial counts
d[1] += 2           # count 2 more of "1"
print(d[1]) # 5

But like Dominik, I do not really know what you’re trying to do.

Cheers,
Cameron Simpson cs@cskk.id.au

I don’t know why you need the global declarations. It is best to avoid

global variables if possible. They make for code that is hard to

maintain.

You can iterate over a set by just iterating over it:

>>> myset = {12, 5, 9, 3, 11, 3}

>>> for i in myset:

...     print(i)

... 

11

9

3

12

5

Notice that:

  • sets cannot contain duplicates;

  • and the order of elements in the set is arbitrary.

So there is no way to ask “give me the third element in the set” because

the order is arbitrary. Obviously when you iterate over the set you will

see the elements in some order, but there is no way to control what

that order is. The order will depend on:

  • the version of Python you are using;

  • which elements are in the set;

  • and the history of the set: removing elements and re-adding them

    can change the order.

If you need to control the order of the elements, use a list. The

disadvantage of a list is that searching is much slower than a set:

[steve ~]$ python3.9 -m timeit -s "data = set(range(100000))" "99999 in data"

10000000 loops, best of 5: 34 nsec per loop

[steve ~]$ python3.9 -m timeit -s "data = list(range(100000))" "99999 in data"

200 loops, best of 5: 1.18 msec per loop

On my computer, searching for 99999 in the list [0, 1, 2, ..., 99999]

is more than 34000 times slower than using a set. That is the cost you

pay to be able to access the elements by index.

If you need indexed access, use a list. If you need fast containment

checking, use a set.

Does this help? If not, please explain in detaik what you are trying to

do, because I don’t really understand your requirements.

I thought that there was a way to do something else not just something that referenced everything again

That to the max thing should be i by now

Hi Jack,

oh, there is, there is… :crazy_face:

Cheers, Dominik

PS: maybe you might want to use google translate or deepl translate?

Sorry Heat Jack, you are making even less sense now than in your first
post. Please remember that we can’t read your mind or see what you are
doing: if you don’t explain it in words, it is a mystery to us.

You say that you thought there was a way to do something else. Of course
there is: whatever you are doing, you can do something else instead.
There is always something else that you can do, if you decide to do
something else instead of what you are doing now.

2 Likes

I guess what I’m asking is how to use a greater than operator with a number of sets

Please show an example of what you are looking for. If your set were {3, 8, 15}, what do you want to know about the elements inside?

how to move from 3 to 8
how to count the number of integers

Counting the elements in the set is via len().

You can get all the elements by iterating over them. But you won’t in general get them in any particular order. You can sort them if that is necessary.

x = {3, 8, 15}
print(f"There are {len(x)} elements in the set.")
for element in x:
    print(f"One element is {element}")
print(f"\nNow the elements in sorted order:")
for element in sorted(x):
    print(f"One element is {element}")

There are 3 elements in the set.
One element is 8
One element is 3
One element is 15

Now the elements in sorted order:
One element is 3
One element is 8
One element is 15

So how do I get {different_element}
What is the equivalent

I’m not sure what you’re asking for. You’re getting all the elements (one at a time) in the iteration. So if you get two, you’ll get a different element.

Do you have one element already known and you want to get some element from the set other than the known one?

Can you give an example of what you want to do with the {3, 8, 15} set? What information do you want returned?

Do you have one element already known and you want to get some element from the set other than the known one?

Yes.

If I have 3 I want 8

Jack, I have no idea what you are asking for. Every time you have
tried to explain what you want, you just make me more confused.

If you want {different_element} then just type it: curly brace, the
element you want, the closing curly brace.

I don’t know what you mean by using > on multiple sets.

If you want help, you have to actually tell us what you are trying to
do. We want to help you, but we can’t read your mind.

You need to spend the time to actually tell us what you want, using
clear examples, and don’t expect us to extrapolate from a few cryptic
statements.

Start with a simple example of your stating values, the result you
want, and how you would get the result. Explain to us like we are 5
years old:

“I want to add two numbers. If I start with the number 1, and the number
2, then I want to add them and get the number 3.”

And now we can tell you the answer, which is the + operator:

1 + 2 returns 3

If you don’t explain what you want in a way we can understand, we can’t
help you, and both we and you will be frustrated.

Jack said:

“If I have 3 I want 8”

Add five to it?

Remember there’s no ordering. So you can get an element that “isn’t 3”, but it might be 8 or it might be 15.

If you have some elements you’ve seen before and don’t want, you can generate the difference set and pick an element out of it.

x = {3, 8, 15}
dontwant = 3

some_other_element = (x - {3}).pop()
print(some_other_element)

8

If the set has no other elements remaining, the pop() will cause an error. You could first check the set and only ask for an element if it has positive size.

You have pop and -

May I ask why you used both?

All depends on whether or not you want to keep the original set around.

I used - to create a temporary set without the element you didn’t want, and then pop() to pull one element out. That leaves the original set unmodified.

pop() picks an element from the set, but that element is removed. If that’s okay, then just pop()ing elements off will get you all the elements. But if you needed the original set in the future, you’d need to save a copy of it.

x = {3, 8, 15}
copy_of_x = x.copy()

print(f"one element from x is {x.pop()}")
print(f"some other element from x is {x.pop()}")
print(f"The rest of the set is now {x}")

one element from x is 8
some other element from x is 3
The rest of the set is now {15}

Cool I use that for one of my other programs

God talking to ya

Have a good day