So i made a two lists:
The first = [0...600] and
the second:`
numbers = -10
listnum = [numbers]
for i in range (1,600,24):
numbers += 1
listnum.insert(i,numbers)
that prints
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
I need to make third list in which will be 600 numbers but the first group of 24 numbers will be replaced by -10, the second one with -9 and so on. I know how to make it hard-way like
for i in range (24):
listnum.insert(i,listnum[0])
for i in range (24,48):
listnum.insert(i,listnum[0])`
But am sure that there is a better way to do that.
You have a math error in OP. I will assume the first list is range(600), and then tell how to get the range(601) version, in case it wasn't an error. Notice that you said it had 600 items, but if you check len(first) you will find it has 601. If you count up from zero to some number, you will have an extra number (because you counted zero). For this reason, the last number in range(600) is 599.
You want count 24 of -10, then count 24 of -9, ... , then count 24 of 14.
My first observation is that the first list is just range(600) or range(601), and it is no different from using a loop counter. So I will not be substituting into it; I will instead just make a new list.
A generic approach would be to just add up sublists with the desired repeated items, formed like so:
l = []
for n in range(-10, 25):
l += [n] * 24
However, since these are all numbers, we can easily compute each value using modulo arithmetic.
[x/24-10 for x in range(600)]
or to match the math error in OP, I think you might want
[x/24-10 for x in range(601)]
which will have an extra 15 at the end.
This way of writing a list is "list comprehension" which you can look up. It is very common. Basically, it will create a new list by computing the quantity "x / 24 - 10" for each item in the iterable, and the iterable is range() which is often used as a counter.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments