tags:

views:

43

answers:

1
  results_histogram_total=list(numpy.histogram(freq,bins=numpy.arange(0,6.1,.1))[0])
  sum_total=sum(results_histogram_total)
  big_set=[]
  for i in results_histogram_total:
    big_set.append(100*(i/sum_total)

is there a shorter way i can write the for loop to append the values?

+4  A: 

For appending, replace the loop with:

big_set.extend(100.0 * i / sum_total for i in results_histogram_total)

however, it's best to replace all the last three lines with just:

big_set = [100.0 * i / sum_total for i in results_histogram_total]

Also, I would advise to not call a list "something set" -- it's very confusing disinformation. But, this is just a bit of naming style advice;-).

Alex Martelli
btw do i need to declare big_set=[] ???
I__
For the former, yes, as you are calling a method (`extend`) of the list, and it needs to be defined in order to exist.For the latter, no, as Python is a dynamically typed language and does not require variables to be declared before they are defined.
Ryan Mentley