How to convert list of floats into strings to append to another list

454 views Asked by At

Everywhere on the web I can find how to convert strings to integers but the opposite does not seem to be working. With this error (argument 2 to map() must support iteration)

My integers:
-0.707106781187
-1.0
-0.408248290464
0.0

The relevant parts of my code:

def calculateZscore(inFileName, outFileName):
    inputFile = open(inFileName,"r")  
    txtfile = open(outFileName, 'w')

for line in inputFile:
  newList = line.strip().split(',')
  obsExp = newList[-2:]
  obsExp = list(map(int, obsExp))
  obs = obsExp[0]
  exp = obsExp[1]
  zScore = (obs - exp) / math.sqrt(exp)
  zScore = list(map(str, zScore))    
  print zScore

if __name__ == "__main__":
   main() 
1

There are 1 answers

0
elethan On BEST ANSWER

It looks like zScore will be a float. As your error message suggests, the second argument to map() must be an iterable (such as a list, str or tuple).

Unless I am missing something, zScore is always a single value, so you could probably get away with simply:

str(zScore)

I am not sure how you intend to use zScore later in your program, but from what you have in your example I don't see any advantage to using map, unless you need to apply str to a list or tuple of values.

However, if you just want your current code to stop raising the error in question, then try replacing this:

zScore = list(map(str, zScore)) 

with this:

zScore = map(str, [zScore]) 

Also, map() will return an iterator, so no need to call list() on the result unless you need to get all the values at once.