Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Total beginner needing some help!!! - please!

Status
Not open for further replies.

Jamfool

IS-IT--Management
Apr 10, 2003
484
GB
I am a complete beginner so please bear with me and any offers of advice and/or tips would be much appreciated!

I have a list which has been inputted by the user. The numbers are any positive and negative integers and zero. I want to include a function where they can turn all the numbers in the list into positive values. (ie The modulus or absolute value) I've been playing aroung with the 'abs' thing but it won't work. I think it's because the data is a list?
 
Loop over the list and call abs with each element, or use map to map the function over the list.
 
Ok, I've tried doing a while loop as suggested.. but it doesn't seem to work? It just makes the first value in the list positive and then returns the rest of the list. I need it to go through the whole list and return the absolute value of each. Please help!! This is what I have so far...

def maximumNorm(self):
i=1
while i<=self.dim():
(self)=abs(self)
return self
 
When you say it was inputted (sic) by the user, do you mean it is a single string with a bunch of numbers in it? Or did you loop and have them successively input numbers?

As far as your code...

( self ) = abs( self )

What the parens on the left hand side? abs returns a scalar, if you want to set something to it then:

self = abs( self )

Also, what is dim()? Is that a method you wrote?

Use the builtin types, they have great power built into them. If you put all your values in an array you would already have a ton of methods and functions to operate on them.

 
1. Indexing starts at 0, not 1.
2. Your return statement is inside the while loop, so your function returns after at most one iteration of the loop.
3. You never increment i.
4. As noted, what is dim?

 
What's the chance you have some VB programming experience?

Since you admit you're an absolute beginner, rather than try to fix the code you've written, why don't you tell us what you're trying to do and we'll tutor you through the "python way" of accomplishing it.
 
Ok "dim()" was something I wrote into the class to return the dimension of the vector. (or the length of the list)
i need this loop to go through each member of the list and return the absolute value and store that in the list.
so...
[-6,-7,4,7,3,-5] would return [6,7,4,7,3,5]
ps: i don't know what vb is? i'm not just a beginner in python.. i've never done any sort of programming ever!
 
Okay... is [-6,-7,4,7,3,-5] a string or an array of numbers?

If it's a array of numbers, then it's simple:

Code:
def abslist( mylist ):
    return [ abs( x ) for x in list ]

Read up on 'list comprehension' to understand what it does.

if it's a string, then you need to convert it to a list with something like

mylist = theinitialstring[1:-1].split( "," )

( [1:-1] says give be the string starting after the first and before the last characters of the string to get rid of the brackets )

but that gives you a list of strings, so using list comprehesion:

mylist = [ int( s ) for s in mylistofstrings ]

Questions?
 
Or
Code:
def abslist( mylist ):
    return map(abs, mylist)
 
Absolutely. In fact, though map is a little less flexible than list comprehension, it is ususally substantially faster. Good to know when you're optimizing for speed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top