Replacing groups of numbers from list (python)

user7879500

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.

Kenny Ostrom

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python replacing numbers in a list

From Dev

Identify groups of continuous numbers from consecutive list in python

From Dev

filtering numbers from Python list

From Dev

Finding groups of increasing numbers in a list

From Dev

Identifying groups of similar numbers in a list

From Dev

Replacing end numbers in a string python

From Dev

Python - replacing string/symbol with numbers

From Dev

Python creating tuple groups in list from another list

From Dev

Python - Replacing characters in a list

From Dev

replacing the values of a list in python

From Dev

Python: Replacing multiple specific words from a list with re.sub

From Dev

Python - Replacing words from list in DataFrame with Regex pattern

From Dev

How to add odd numbers from a list in python?

From Dev

Python printing floating point numbers from a list

From Dev

Finding the 5 smallest numbers from a list in Python

From Dev

Rearranging numbers from list in python3

From Dev

Replacing from dictionary - Python

From Dev

Replacing roman numerals with numbers from file- Python- type error

From Dev

Replacing roman numerals with numbers from file- Python- type error

From Dev

Replacing Values in a List with Index in Python

From Dev

Replacing duplicate elements in a list in Python?

From Dev

Replacing python list elements with key

From Dev

Replacing elements in long list Python

From Dev

Replacing parts of strings in a list in Python

From Dev

Cost of replacing an element of a list in python

From Dev

Replacing part of a string in a list in python

From Dev

Read a python list and remove certain numbers from given list

From Dev

Getting groups as a list from arraylist

From Dev

replacing values from file python

Related Related

HotTag

Archive