Regarding question on coding - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Regarding question on coding (/thread-27097.html) |
Regarding question on coding - asmfloyd - May-26-2020 Hi, I am learning python. I just saw this coding which is generating 2 datasets.I have a question. Can someone explain the part in "dataset1"? I believe it is a list comprehension. Mainly I am not understanding what "for child in children" does? import numpy as np np.random.seed(10) children=range(10) months=np.arange(13) dataset1=[ (month, np.dot(month,24.7) + np.random.normal(loc=0,scale=20)) for month in months for child in children] month_data=[element[0] for element in dataset1] # Gets 1st column from data weight_data=[element[1] for element in dataset1]Thanks, RE: Regarding question on coding - deanhystad - May-26-2020 Comprehensions can be unwound. Start at the end and work your way back to the assignment. Use the same process, but in reverse, to convert for loops to a comprehension. for child in children: for month in months: dataset1.append( (month, np.dot(month, 24.7)+np.random.normal(loc=0, scale=20)) ) RE: Regarding question on coding - buran - May-26-2020 (May-26-2020, 01:04 AM)asmfloyd Wrote: I am not understanding what "for child in children" does?children is range object. by iterating over children , child value is changing from 0 to 9. It looks the purpose is to have 10 values in the dataset for each month value
|