Home > OS >  Python list with zeros and numbers repeating
Python list with zeros and numbers repeating

Time:03-30

My intent is to generate a generic empty list and then to append it with a numeric sequence such that it gives zeros but at the third place it gives 3 and then its multiples, that is a[(0,0,3,0,0,6,0,0,9)] and it needs to have 100 values inside. I first set the list and then I use the 'for' loop in a range(0,100) and I am sure I need to use % in ways such that whenever my sequence going 1 to 100 is perfectly divisible by 3 it gives back 3 (and not 0) but then it keeps evolving in 6,9,12.. How do I do?

for i in range(0,100):
    if i%3==0:
        return 0
    else
        return 3

Of course this is completely wrong but i am new to programming in general. Thanks in advance.

CodePudding user response:

you could try this:

for i in range(0, 100, 3):
    list[i]=i

You just change the "step" of the range function, so the index i will represent also the value passed in the list, it should work properly.

CodePudding user response:

@Mark's comment is very relevant and makes good use of the modulo and list comprehension properties in a very simple way. Moreover, its code easily adapts to any value.

However the modulo operator is quite slow so here are other ways to achieve the same result.


Method 1

We can make a range from 3 to 101 with a step of 3, and add [0, 0, i] to each step. Since there will be missing zeros at the end of the list, we must add as many as the rest of the division of 100 by 3.

data = [num for i in range(3, 101, 3) for num in [0, 0, i]]   [0] * 1

Method 2

With the same idea, we can use .extend() to add two 0s before each term.

data = []
for i in range(3, 101, 3):
    data.extend([0, 0, i])
data.append(0)

Method 3

The simplest idea, we create a list of 100 zeros, and then we modify the value every 3 terms.

data = [0] * 100
for i in range(2, 101, 3):
    data[i] = i   1

Comparison

Using timeit, here is a comparison of the speed of each algorithm, the comparison is based on 10000 repetitions.

import timeit

print(timeit.timeit("data = [0 if n % 3 else n for n in range(1, 101)]"))
print(timeit.timeit("data = [num for i in range(3, 101, 3) for num in [0, 0, i]]   [0] * 1"))
print(timeit.timeit("""
data = []
for i in range(3, 101, 3):
    data.extend([0, 0, i])
data.append(0)
"""))
print(timeit.timeit("""
data = [0] * 100
for i in range(2, 101, 3):
    data[i] = i   1
"""))

Output:

4.137781305000317
3.8176420609997876
2.4403464719998738
1.4861199529996156

The last algorithm is thus the fastest, almost 3 times faster than using modulus.

CodePudding user response:

A function stops running, when it encounters a return. So in your code, you only ever execute the loop once.

But, what if we could change that? Do you know what a generator is? Have a look at this:

def multiples_of_three_or_zero():
    for i in range(0,100):
        if i%3==0:
            yield 0
        else
            yield 3

That's a generator. yield doesn't end the execution, but rather suspends it. You use it like this:

for i in multiples_of_three_or_zero():
    print(i)

Or, if you really want all the elements in a list, just make a list from it:

list(multiples_of_three_or_zero())
  • Related