python - how to break down a str into a list -


this i've done far:

def file_to_dict(f):  """ (file open reading) -> dict of {float: int}  f contains exchange rate changes floating point numbers separated whitespace. return dict exchange rate changes keys , number of occurrences of exchange rate changes values. """  file = open(f, 'r') data = list(file.read().strip().split('\n')) 

the data is:

0.0045  0.0160 -0.0028 -0.0157 -0.0443 -0.0232 -0.0065 -0.0080  0.0052 -0.0052 -0.0283 -0.0087 -0.0020 -0.0080 -0.0065 -0.0290  0.0180  0.0030 -0.0170  0.0000 -0.0185 -0.0055  0.0148 -0.0053  0.0265 -0.0290  0.0010 -0.0015  0.0137 -0.0137 -0.0023  0.0008  0.0055 -0.0025 -0.0125  0.0040 

how make each number item in list? eg: [0.0045, 0.0160, etc...] or ['0.0045', '0.0160', etc...]

something this?

>>> open('filename', 'r') f:         newlist = []         line in f:             newlist.extend(map(float, line.split()))   >>> newlist [0.0045, 0.016, -0.0028, -0.0157, -0.0443, -0.0232, -0.0065, -0.008, 0.0052, -0.0052, -0.0283, -0.0087, -0.002, -0.008, -0.0065, -0.029, 0.018, 0.003, -0.017, 0.0, -0.0185, -0.0055, 0.0148, -0.0053, 0.0265, -0.029, 0.001, -0.0015, 0.0137, -0.0137, -0.0023, 0.0008, 0.0055, -0.0025, -0.0125, 0.004] 

since, can't use map(), like

>>> open('filename', 'r') f:         newlist = []         line in f:             elem in line.strip().split():                 newlist.append(float(elem)) 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -