Python中有布尔函数的快捷方式吗?

2024-06-26 00:12:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道在PHP或Java中,你可以做这样的事情

int usefulCharacters = text == null ? 0 : text.length();

因此,在Python中,我想检查在使用filter(item,iterable)函数之后,如果item出现在iterable中,则返回true:

注意:有效的索引是rdd格式的,所以我必须将其转换为列表,这样它才是可编辑的

is_index1 = (filter(index_md5,valid_indices.collect()) ? true:false)

但是Python似乎不喜欢“?”语法。请告知,我正在学习Python,谢谢


Tags: 函数texttrue格式javafilteriterableitem
2条回答

您可能正在寻找这个(df是PySpark数据帧,而不是RDD

from pyspark.sql import functions as F
from pyspark.sql.functions import col, size

Res=df.select(F.when(F.col('text').isNull(), lit(0)).otherwise(F.size(F.col('text'))).alias('new_col'), <other columns>)

在python中,三元运算符使用语法: True if <condition> else False

因此,根据您的代码示例,它将给出:

is_index1 = True if filter(index_md5,valid_indices.collect()) else False

注意,我还使用了TrueFalse值来使用Python语言布尔值

相关问题 更多 >