+ 1

How lst = [float(x) if x != 'nan' else np.NaN for x in input().split()] works

Hello, i wanna ask about something I wanna know how this code works, it actually has been used for lots of question in datascience project in sololearn lst = [float(x) if x != 'nan' else np.NaN for x in input().split()] One of the full code example down here :. Import numpy as np Import pandas as pd lst = [float(x) if x != 'nan' else np.NaN for x in input().split()] lst_df = pd.Series(lst) Sylv = lst_df.fillna(lst_df.mean()).round(1) print(Sylv)

9th Apr 2021, 8:39 AM
Sylvanast
Sylvanast - avatar
5 ответов
+ 1
lst = [float(x) if x != 'nan' else np.NaN for x in input().split()] Its generator And equal this: lst =[] For x in input().split(): if x =! 'nan': lst.append(float(x)) else: lst.append(np.NaN)
9th Apr 2021, 9:41 AM
Илья Мирошник
Илья Мирошник - avatar
+ 2
list comprehension: https://www.sololearn.com/learning/2454/ cant post a link that works so look for it in python tutorial
9th Apr 2021, 8:57 AM
Slick
Slick - avatar
+ 1
input().split() its splitted into list input from user (by spaces in default)
9th Apr 2021, 9:43 AM
Илья Мирошник
Илья Мирошник - avatar
+ 1
if on input u have "2 1 3 nan" on output u will have [2.0, 1.0, 3.0, NaN]
9th Apr 2021, 9:45 AM
Илья Мирошник
Илья Мирошник - avatar
0
Илья Мирошник Thank you xD, it helps lot
9th Apr 2021, 10:52 AM
Sylvanast
Sylvanast - avatar