0

My file looks like this:

A matrix of 2 by 100 ,

I would like to create a list for each column one list which correspond to the first element of each row mapping to current and the second element maps to the temperature.

As shown below. any better way to make the code look fancier?

-12,30
-34,50
-33,89
-900,9
-2,37
-7,17
-8,28
-12,30
-34,50
-33,89
def parse_log(fname):
 f = open(fname, "r")
 samples = f.readlines()
 samples = filter(lambda x: not x.startswith('*'), samples)
 print(samples)
 current = map(lambda x: -1 * int(x.split(',')[0]), samples)
 print(current)
 temperature = map(lambda x: int(x.split(',')[1]), samples)
 print(temperature)
 return (current, temperature)
shobhit
7002 gold badges9 silver badges21 bronze badges
asked Sep 18, 2011 at 3:11
1
  • avoid map and filter with lambda. Use comprehensions. Commented Sep 18, 2011 at 3:15

3 Answers 3

1

To avoid doing the split call twice for every line, I'd suggest the following solution

def parse_log(fname):
 with open(fname) as f:
 samples = [line.strip() for line in f.readlines()
 if not line.startswith('*')]
 ints = [map(int, line.split(",")) for line in samples]
 currents = [-x[0] for x in ints]
 temperatures = [x[1] for x in ints]
 return currents, temperatures
answered Sep 18, 2011 at 8:14
Sign up to request clarification or add additional context in comments.

Comments

1

This is a simple version that would be IMO reasonable up to a few megabytes of log file (it doesn't try to minimize memory usage or computing time during parsing):

 def parse_log(fname):
 data = [map(int, x.split(",")) for x in open(fname) if x[:1] != "*"]
 return ([-current for current, temp in data],
 [temp for current, temp in data])
answered Sep 18, 2011 at 8:26

Comments

0

Using generator expressions:

def parse_log(fname):
 with open(fname, "r") as file:
 return zip(*(
 (int(current) * -1, int(temp)) for current, temp in
 (line.strip().split(',')
 for line in file if not line.startswith('*'))
 ))
print parse_log(filename)
[(-12, -34, -33, -900, -2, -7, -8, -12, -34, -33), (30, 50, 89, 9, 37, 17, 28, 30, 50, 89)]

Warning, this isn't necessarily better as it's probably harder to read and understand what's going on. Be sure to document it properly with a docstring.

answered Sep 18, 2011 at 3:36

4 Comments

Your answer doesn't use any list comprehensions. You also missed correcting the negative value on the currents.
Sorry, changed my answer but not my text. Fixed the text :)
Ah, he didn't put that in his requirements and I overlooked it in the code.
zip(*((abs(int(current)), int(temp)) for current, temp in (line.split(',') for line in f if not line.startswith('*'))))

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.