[英]How to generate the Python lambda filter codes in the for loop?
I am a beginner to Python lambda.我是 Python lambda 的初学者。 And try to convert the Python for loop to lambda expression.
并尝试将 Python for loop 转换为 lambda 表达式。 First I would like to explain the for loop lines.
首先,我想解释一下 for 循环行。
fred = Fred2Hdfs() # construct the python imported objects
for i, state in enumerate(us_states):
df_unemployee_annual = fred.getFredDF('A', state, 'search_text') # generate dataframe from the object
if df_unemployee_annual is None:
continue
if i == 0:
fred.writeCsv2Hdfs('unemployee_annual.csv', df_unemployee_annual) # write dataframe
else:
fred.appendCsv2Hdfs('unemployee_annual.csv', df_unemployee_annual) # append dataframe
The above code work successfully without errors.上面的代码成功运行,没有错误。 And below codes are the Python lambda codes which I try to convert.
下面的代码是我尝试转换的 Python lambda 代码。
fred = Fred2Hdfs()
freq='A'
str='search_text'
result_df_list = list(map(lambda state: fred.getFredDF(freq, state, str), us_states))
result_df_list = list(filter(lambda df: df is not None, result_df_list))
print(result_df_list) # codes work correctly until this line.
#func=map(lambda df:fred.writeCsv2Hdfs('unemployee_annual_.csv', df) , result_df_list)
I am stuck with if i==0:
line in the for loop.我坚持
if i==0:
for 循环中的行。 How can I make the appropriate Python lambda expression from if i==0:
line.如何从
if i==0:
行制作适当的 Python lambda 表达式。 I am afraid I have no idea how to implement the if filter of Python lambda.恐怕我不知道如何实现 Python lambda 的 if 过滤器。
map(lambda (i,df):fred.writeCsv2Hdfs('unemployee_annual_.csv', df) if i == 0 else fred.appendCsv2Hdfs('unemployee_annual_.csv', df) , enumerate(result_df_list))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.