1

I am having trouble with list comprehension in Python

Basically I have code that looks like this

output = [] for i, num in enumerate(test): loss_ = do something test_ = do something else output.append(sum(loss_*test_)/float(sum(loss_))) 

How can I write this using list comprehension such as:

[sum(loss_*test_)/float(sum(loss_))) for i, num in enumerate(test)] 

however I don't know how to assign the values of loss_ and test_

9
  • Have you tried to do that at all? What happened? Where's the code you're actually "having trouble with", and what precisely do you mean by trouble? A minimal reproducible example would help. Commented Aug 19, 2015 at 14:26
  • Why? What's wrong with the for loop you have now? Commented Aug 19, 2015 at 14:29
  • In Python 2, instead of doing c = a / float(b), do a from __future__ import division; c = a / b, see this PEP. This is the default in Python 3. Commented Aug 19, 2015 at 14:29
  • @BasSwinckels thanks Commented Aug 19, 2015 at 14:32
  • @MorganThrapp Nothing is wrong with the current code, I would just like to convert into the list comprehension format Commented Aug 19, 2015 at 14:33

3 Answers 3

3

You can use a nested list comprehension to define those values:

output = [sum(loss_*test_)/float(sum(loss_)) for loss_, test_ in ((do something, do something else) for i, num in enumerate(test))] 

Of course, whether that's any more readable is another question.

Sign up to request clarification or add additional context in comments.

Comments

1

As Yaroslav mentioned in the comments, list comprehensions don't allow you to save a value into a variable directly.

However it allows you to use functions.

I've made a very basic example (because the sample you provided is incomplete to test), but it should show how you can still execute code in a list comprehension.

def loss(): print "loss" return 1 def test(): print "test" return 5 output = [loss()*test() for i in range(10) ] print output 

which is this case will result in a list [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

I hope this somehow shows how you could end up with the behaviour that you were looking for.

2 Comments

I think the question is not so much "how to execute complex stuff in a list comprehension" but "how to calculate a temp variable once and use it in two places", as OP does with loss_.
@tobias_k explained it better than I could explain it myself
0
ip_list = string.split(" ") # split the string to a list using space seperator for i in range(len(ip_list)): # len(ip_list) returns the number of items in the list - 4 # range(4) resolved to 0, 1, 2, 3 if (i % 2 == 0): ip_list[i] += "-" # if i is even number - concatenate hyphen to the current IP string else: ip_list[i] += "," # otherwize concatenate comma print("".join(ip_list)[:-1]) # "".join(ip_list) - join the list back to a string # [:-1] trim the last character of the result (the extra comma) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.