Hello,
I am working on a code that takes the users input ( a string of digits) from the command line and prints out the median number. I am trying to accomplish this by:
from sys import argv
nums = argv[1:]
for index, value in enumerate(nums) :
nums[index] = float(value)
print nums[1:] + nums[:1] / 2
I keep getting the error:
type error: unsupported operand type(s) for /: 'list' and 'int'
2 3491 Laharl 849
Recognized Expert Contributor
Slicing always returns a list, even if that list only has one operator. As such, nums[1:] is a list, as is nums[:1].
The builtin sum(list, start=0) function will probably help you here.
bvdet 2,851
Recognized Expert Moderator Specialist
Hello,
I am working on a code that takes the users input ( a string of digits) from the command line and prints out the median number. I am trying to accomplish this by:
from sys import argv
nums = argv[1:]
for index, value in enumerate(nums) :
nums[index] = float(value)
print nums[1:] + nums[:1] / 2
I keep getting the error:
type error: unsupported operand type(s) for /: 'list' and 'int'
First, let's consider the definition of the median number of a sequence of numbers. The median is the number where half of the numbers are larger and half are smaller. Therefore the list must be sorted. If the list contains an even number of items, the median is taken as the mean or average between the two middle numbers. The following function returns the median number from a sorted list of numbers: - def median(s):
-
i = len(s)
-
if not i%2:
-
return (s[(i/2)-1]+s[i/2])/2.0
-
return s[i/2]
Example: - >>> s1 = "100 45 89 23 105 220 45 66 8 75"
-
>>> s2 = "100 45 89 23 105 220 45 66 8 75 7"
-
>>> sList = [float(num) for num in s1.split()]
-
>>> sList.sort()
-
>>> median(sList)
-
70.5
-
>>> sList
-
[8.0, 23.0, 45.0, 45.0, 66.0, 75.0, 89.0, 100.0, 105.0, 220.0]
-
>>> sList = [float(num) for num in s2.split()]
-
>>> sList.sort()
-
>>> median(sList)
-
66.0
-
>>> sList
-
[7.0, 8.0, 23.0, 45.0, 45.0, 66.0, 75.0, 89.0, 100.0, 105.0, 220.0]
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: lecichy |
last post by:
Hello
Heres the situation:
I got a file with lines like:
name:second_name:somenumber:otherinfo
etc with different values between colons ( just like passwd file)
What I want is to extract some part of it like all names or numbers from
each line, simply text fom between e.g. second and third colon. And turn it
|
by: Eric Linders |
last post by:
Hi,
I'm trying to figure out the most efficient method for taking the
first character in a string (which will be a number), and use
it as a variable to check to see if the other numbers in the string
match that first number. I'm using this code for form validation of a
telephone number.
Previous records from the past few months show that when someone is
just messing around on one of our forms (to waste our time), they type
|
by: Bob |
last post by:
I have been looking at the code for MedianFind(pDte As String) from the
following thread from UtterAccess.com: "Finding Median average grouped
by field"
I have been able to get it to run using Northwind no problem. I am
having some trouble though converting it to my specific purpose which
may be less complicated than the solution Bob (raskew) provided in the
thread.
Here are my specifics:
|
by: Tarun |
last post by:
Hi All,
I need to find a particular substring in a binary string(some text
appended & prepended to binary data).
I cant use strstr since it terminates on receiving '\0'which can be
there in binary data also I cant use memmem. Is there any other
available function to do this.
Regards
Tarun
|
by: karthi |
last post by:
hi,
I need user defined function that converts string to float in c.
since the library function atof and strtod occupies large space in
my processor memory I can't use it in my code.
regards,
Karthi
| |
by: suhdominic |
last post by:
Can anyone help me how to program in C++ which determines the median of five input numbers?
|
by: Storm9 |
last post by:
I have to:
"Use functional decomposition to write a C++ program that determines the median of five input numbers. The median is the middle number when the five are arranged in order. However, the user can input the values in any order, so your program must determine which value is between the other two. For example, if the user enters:
41.52 27.18 96.03 12.5 13.8
then the program would output:
The median of 41.52, 27.18, 96.03, 12.5,...
|
by: Bhadan |
last post by:
Hello,
I have several jagged arrays which have been sorted.
I'm trying to find the median of each array. Any tips appreciated.
TIA.
Bhads.
|
by: mehwishobaid |
last post by:
i dont know wat is wrong with my code. when i compile. i get the error
saying line 29: error: expression must have pointer-to-object type
#include <iostream>
using namespace std;
#include <vector>
double* median(double a, int a_size) // Function Median
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |