I am trying to understand the following one-liner: weights = [0.0 for i in range(len(training_data[0]))]
I would like to understand what that actually says. I think it says Append a [0.0] to the weights
list for every i
in the range of the length of training_data[0]
I need to convert that one-liner to a normal for loop. The following does not yield the same result, as the program I editing later complains about a numpy problem:
weights = []
num_rows = training_data[0]
for index in range(len(num_rows)):
weights.append([0.0])
Any advice on how to convert the one-liner to a normal looking for loop is appreciated.
It's a list comprehension, basically you convert it like this:
weights = [0.0 for i in range(len(training_data[0]))]
into:
weights = []
total_iterations = len(training_data[0])
for i in range(total_iterations): # equivalent to for i==0, i<total_iterations, i++ in other languages
weights.append(0.0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.