Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The use of enumerate
#1
Hello,

Trying to substract one index by another.

def is_prime_v1(n):
    """Return a boolean indicating if integer n is prime"""
    if n == 1:
     return False
      
    for d in range(2, n):
        if n % d == 0:
            return False
    return n
 
prime = []
for n in range(1,20):
  if is_prime_v1(n):
    print(is_prime_v1(n))
    prime.append(n)
 

for idx, pr in enumerate(prime):
    print((pr[idx]+1) - pr[idx])
    
 
Of course you cannot do that. There is probably a way. I have been searching on the net but its always the same examples that are coming back.
Any pointers?

TY
Reply
#2
I think you're trying to subtract the value of one prime in your list from the next, but you don't say that in your post.

Here you might be confusing pr one of the primes for prime the list of primes. There's a couple of ways to do that. One is to actually use the indicies. When you only need one of the elements at a time, you shouldn't do that. But here I don't mind it as much.

for idx in range(len(prime) - 1):
    print(prime[idx + 1] - prime[idx])
Another way is to just construct 2 lists from your list of primes, one at an offset. Then you don't need to track indices and just subtract the elements as they're seen.

for x, y in zip(prime, prime[1:]):
    print(y - x)
Frankduc and BashBedlam like this post
Reply
#3
I tried the first version before asking and end up with out of range error. Maybe i misplaced the -1
I like the second version better. Thank you for your kind response.
Reply
#4
The first version works. When things don't work you should try determining why.

The way you are using enumerate() is odd. enumerate() is a generator. You pass it an iterable and it generates a series of tuples. You could write enumerate yourself.
numbers = "One", "Two", "Three"

def my_enumerate1(iterable):
    index = 0
    for value in iterable:
        yield index, value
        index += 1

def my_enumerate2(iterable):
    for pair in zip(range(len(iterable)), iterable):
        yield pair

print("enumerate", list(enumerate(numbers)))
print("my_enumerate1", list(my_enumerate1(numbers)))
print("my_enumerate2", list(my_enumerate2(numbers)))
Output:
enumerate [(0, 'One'), (1, 'Two'), (2, 'Three')] my_enumerate1 [(0, 'One'), (1, 'Two'), (2, 'Three')] my_enumerate2 [(0, 'One'), (1, 'Two'), (2, 'Three')]
I don't use enumerate very often. Either I want to treat a list like a collection of values, or I want to treat it like an indexed array. It is not often that I want to do both.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in using enumerate akbarza 4 800 Oct-11-2023, 10:55 AM
Last Post: perfringo
  Need to understand the way enumerate() function works abhishekambastha 2 2,491 Sep-16-2018, 08:21 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020