3

I need to implement Sieve of Eratosthenes algorithm. I have list:
bar = [2, 3, 4, 5, 6, 7, 8, 9, 10]
I need to replace each odd element with "0". Now I have code:

while viewIndex < maxNumber:
    bar[viewIndex] = 0
    viewIndex += 2

But I remember about slices. And for me it will be nice to write something like this:

bar[currentIndex::2] = 0

But I have error:

TypeError: must assign iterable to extended slice

Maybe you know a beautiful solution to this task.

3
  • 1
    Have a look at this question stackoverflow.com/questions/11395057/… Commented Dec 4, 2017 at 13:40
  • If you're looking to build a Sieve of Eatosthenes, there is one here. Not sure what you seek to accomplish simply by skipping the odd indices. The Sieve will take care of all things for you. Commented Dec 4, 2017 at 14:18
  • numpy lets you do this directly. would be nice if core python did too. Commented Mar 21, 2019 at 13:14

5 Answers 5

5

You should assign the slice to an iterable of the same length as the number of odds:

bar[1::2] = [0]*(len(bar)//2)
print(bar)
# [2, 0, 4, 0, 6, 0, 8, 0, 10]

To extend this for even indices, you need to take into consideration lists with odd-lengths (not relevant for the above case) by adding the modulo 2 value of the list length:

bar[::2] = [0]*(len(bar)//2 + len(bar)%2)

Which is same as:

bar[::2] = [0]*sum(divmod(len(bar), 2))
Sign up to request clarification or add additional context in comments.

18 Comments

What does the :: do? Not familiar with that.
@pstatix Python's slice syntax: start:stop:stride
You don't necessarily need to use the :: though do you? A single : will work, at least it appears so in the post from @RoachLord.
@pstatix you need it: slice starts at index 1, goes to the end (omitted, hence consecutive colons), in steps of 2. Equivalent to bar[1:len(bar):2]
@schwobaseggl Excellent, got it.
|
1

I use numpy:

foo = np.ones(10)
foo[1::2] = 2

This just works.

You shouldn't have to keep track of indices -- the point of slicing is to make things more convenient, not to force you to keep track of which day of the week relative to the third Tuesday of last month you bought bread.

Comments

0

Use simple for loop

bar = [2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in  range(len(bar)):
   if bar[i] % 2 != 0:
       bar[i] = 0
print(bar)

Output

[2, 0, 4, 0, 6, 0, 8, 0, 10]

Comments

0

You can use map to set elements on odd index to zero,

bar = [2, 3, 4, 5, 6, 7, 8, 9, 10]
print map(lambda i: 0 if bar.index(i)%2!=0 else i, bar)

[2, 0, 4, 0, 6, 0, 8, 0, 10]

or if you want to set odd element value to zero, you can do this,

map(lambda i: 0 if i%2!=0 else i, bar)

4 Comments

In your case as a result I will have iterator. But I need list
What version of python are you using? i am sure i you are using python 2, map returns a list type not iterator.
Now I use python 3.5 but I have read books about python 2. Thank you a lot for answer!
You can explicitly convert a map object to list like list(map(lambda i: 0 if i%2!=0 else i, bar))
0

Thank to all for answers. My implementation of Sieve of Eatosthenes algorithm:

def resheto(data): 
     print("\tStart Resheto") 
     currentIndex = 0 

     while currentIndex < len(data) - 1: 
         data[currentIndex + data[currentIndex]::data[currentIndex]] = \
             [0] * ((len(data) + 1) // data[currentIndex] - 1) 
         currentIndex += 1 

         while currentIndex < len(data) - 1 and data[currentIndex] == 0: 
             currentIndex += 1 

         if currentIndex >= len(data) - 1: 
             break 
print("\tEnd Resheto") return data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.