How to create list from 100 elements to list of 10 [duplicate]

2024/11/16 9:44:22

I have small brain fade today and I believe it will be faster to get hint here than wondering for an hour.

I have list A of 100 elements. How can I create another list(B) basing on list A, that will have 10 elements, so that each element is mean of 10 elements from list A?

Thank you in advance.

e.g.A = [10,10,10,10,10,20,20,20,20,20,30,30,30,30,30,40,40,40,40,40]
B = [15, 35]
Answer
A = [i for i in range(100)]
[sum(A[k * 10 : 10*(k+1)])/10 for k in range(len(A)//10)]

Output

[4.5, 14.5, 24.5, 34.5, 44.5, 54.5, 64.5, 74.5, 84.5, 94.5]
https://en.xdnf.cn/q/118942.html

Related Q&A

how to make the width of image longer using animation in tkinter GUI?

I have this image and I want to make the width of this image longer till the end of the window of tkinter using animation but I havent got any proper way to achieving this task. any suggestions ?

Syntax error with if statement in python 2.7

Im having trouble with a "Syntax Error: invalid syntax" message in python code that keeps moving the goals posts on me. Heres the sample code:another_answer = int(raw_input("> "…

Python- Quicksort program in-place

I have a quicksort program here, but there seems to be a problem with the result. I think there must have been some issue in the areas highlighted below when referencing some values. Any suggestions?#…

Maximum recursion error in `__eq__` implementation

class Coordinate(object):def __init__(self,x,y):self.x = xself.y = ydef getX(self):# Getter method for a Coordinate objects x coordinate.# Getter methods are better practice than just accessing an attr…

python swig : ImportError wrong ELF class: ELFCLASS64

I am trying to interface from python to c++ code via swig. I get the following error , while trying to execute my script.File "./driver.py", line 4, in <module>from fixMessageSim import…

cnn news webscraper return empty [] without information

so i wrote this code for now: from urllib import request from bs4 import BeautifulSoup import requests import csv import reserch_term = input(What News are you looking for today? )url = fhttps://editi…

Why the code shows all the addition process?

Code: sum=0 for i in range(10,91):sum=sum+iprint(sum)When I wrote this code, the answer was Output: 10 21 33 46 60 75 91 108 126 145 165 186 208 231 255 280 306 333 361 390 420 451 483 516 550 585 621 …

Creating a list of keywords by scrolling through a dataframe (python)

I have a dataframe that looks like this: dataFrame = pd.DataFrame({Name: ((" Verbundmrtel , Compound Mortar , Malta per stucchi e per incollaggio "),(" StoLevell In Absolute , StoLeve…

How to click this button with python selenium

Im looking to click the button highlighted in the screenshot below; have tried with pyautogui but found results to be inconsistent so trying selenium instead.Im having trouble identifying the button to…

How to find the average of numbers being input, with 0 breaking the loop?

I just need to figure out how to find the average of all these input numbers by the user while using 0 as a exit of the loop. I need to figure out how to eliminate using 0 as part of the average. examp…