[英]What does a variable before a foreach loop mean when enclosed in square brackets (PySpark)
I am still at the initial phases of learning pyspark, and I found below code confusing.我仍处于学习 pyspark 的初始阶段,我发现下面的代码令人困惑。 Can someone, please explain this to me
有人可以向我解释一下吗
Code :
zipsSchema = smartphoneDF.schema
print(type(zipsSchema))
[**field** for field in zipsSchema]
What does "field" before "for" mean, and why is the entire statement enclosed in "[]" When I try to write the same
My code is as follows :
zipsSchema = smartphoneDF.schema
print(type(zipsSchema))
for field in zipsSchema
print(field)
field
is an element from the zipsSchema struct type. field
是来自zipsSchema结构类型的元素。
Rather than creating empty list and addint elements to the list we can write for loop in single line using [field for field in zipsSchema]
is list comprehension in python.我们可以使用
[field for field in zipsSchema]
中的列表理解,而不是创建空列表和向列表添加元素,而是在单行中编写 for 循环。
Example:
smartphoneDF=spark.createDataFrame([("1","a")],["id","name"])
zipsSchema=smartphoneDF.schema
fields_lc=[field for field in zipsSchema]
#[StructField(id,StringType,true), StructField(name,StringType,true)]
fields_fl=[]
for field in zipsSchema:
fields_fl.append(field)
fields_fl
#[StructField(id,StringType,true), StructField(name,StringType,true)]
As you can see fields_lc
, fields_fl
values are same and creation is different.正如您所看到
fields_lc
, fields_fl
值是相同的,而创建是不同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.