![]() |
|
how to get average of numeric values from string in list? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5) +---- Thread: how to get average of numeric values from string in list? (/showthread.php?tid=6260262) |
how to get average of numeric values from string in list? - bigfatjoe - Jan 5, 2024 I tried splitting each value up into two values and appending average of each two values into an array and casted to integers. its giving me an error. Here is code: Code import numpy as npError I am getting: Code greg@greg-IdeaPad-5-15ARE05:~$ /bin/python3 "/home/greg/Documents/Python/Day 20.py"This is for exercise 2 of Day 20 of the 30 Days of Python course: https://github.com/Asabeneh/30-Days-Of-Python/blob/master/20_Day_Python_package_manager/20_python_package_manager.md RE: how to get average of numeric values from string in list? - Dirty - Jan 5, 2024 Never used bumpy but from reading the docs and a few stackoverflow posts, my first thing I would try is having a value inside weight lists. This function is to concatenate arrays together. Or a value to an array I guess although for that there is simply better things. In this case you have a empty array and just a value which is what it's triggering over I think. RE: how to get average of numeric values from string in list? - Perlo - Jan 8, 2024 You are looking for a similar function to pythons list append, which simply adds something to the end an existing list. np.concatenate isnt the correct function for this. You are better off using numpy's np.append(), which takes in the list to append to and the object to add. In addition to that, since we are using numpy its very easy to use the built in functions (such as np.mean, np.min, np.max, np.std). Also, you can remove the int() in your way of calculating the average, since you already converted the 2 numbers to integers, and divided by another integer, meaning we already have an integer. Meaning value = int((first_num + second_num)/2) can be written shorter as: value = (first_num + second_num)/2. Also even shorter in the notation below using only numpy. Code import numpy as npRE: how to get average of numeric values from string in list? - snoopy717 - Jan 8, 2024 It looks like you are trying to use np.concatenate incorrectly. The np.concatenate function requires at least two arrays to concatenate. Additionally, it's better to use np.append in this case, as you are adding elements one by one. Here's the corrected code: Code import numpy as npIf you have any question, PM me RE: how to get average of numeric values from string in list? - bigfatjoe - Jan 8, 2024 I solved it before you guys posted this with a little hint or two from a friend. Here is right code: Code cats_api = requests.get('https://api.thecatapi.com/v1/breeds')RE: how to get average of numeric values from string in list? - siliconvalley - Apr 3, 2024 Try this: import numpy as np import requests cats_api = requests.get('https://api.thecatapi.com/v1/breeds') cats = cats_api.json() weight_list = np.array([]) for cat in cats: num = cat["weight"]["metric"] first_num = int(num.split("-")[0]) second_num = int(num.split("-")[1]) value = int((first_num + second_num) / 2) weight_list = np.concatenate((weight_list, np.array([value]))) print(weight_list) |