【问题标题】:How to convert "true" and "false" to their boolean values?如何将“真”和“假”转换为它们的布尔值?
【发布时间】:2022-01-04 20:51:01
【问题描述】:

我需要一个名为 REPORTED [" true "," false "," true "," false "," false "] 的 bool 列表,将其转换为 bool: [true, false, true] 类型,然后将其置于我执行以下代码的条件下,该代码试图根据此列表中的 2 个列表获取总数的平均百分比案例s.Body [Physical appearance]s.Reported [true: reported]即:计算有多少人有身体方面[fat, muscular, skinny]不是Reported [false]并得到每个方面的百分比

我的问题是,如果不在 bool 中,我不需要“true”或“false”(在 str 中),而且我不知道如何设置条件来识别我认为我转换的 bool

def porcentaje (Doc):
    Datos2 = [s.Cuerpo for s in Doc if s.DENUNCIADO ] #
    ConvertidorBool = list(map(lambda ele: ele == "false", Datos2))
    Datos=[s.Cuerpo for s in Doc if ConvertidorBool == "false"] # condicion
    CC = {Ca: Datos.count(Ca)*100/1000 for Ca in Datos if Ca !=""}
    Contador = Counter(CC)
    Agrupador = Contador.items()
    Porcentaje = dict(Agrupador)
    Resultado= print("{}".format(Porcentaje))
    return Resultado

【问题讨论】:

  • booled_result = [True if _ == "true" else False for _ in origional_data]

标签: python csv


【解决方案1】:

你可以使用ast.literal_eval:

>>> import ast
>>> ast.literal_eval(" true ".strip().title())
True
>>> ast.literal_eval(" false ".strip().title())
False

看起来您只需要去掉值中任何周围的空格并将它们正确大写以创建完全包含 bool 文字的字符串。

但是,literal_eval 不关心类型,所以如果您的列表可能包含非布尔文字,例如 "5""'foo'",那么 literal_eval 会很乐意为您创建 5'foo' .在这种情况下,显式比较可能是一个更好的主意(也更有效)。

【讨论】:

  • 谢谢你的帮助,谢谢!!
【解决方案2】:

如果你知道数据都是[" true "," false "," true "," false "," false "]你可以这样做:

li=[" true "," false "," true "," false "," false "]
>>> [True if "true" in s else False for s in li]
[True, False, True, False, False]

或者:

>>> [True if s.strip()=="true" else False for s in li]
[True, False, True, False, False]

# [s.strip()=="true" for s in li] works too...

如果源列表中有不相关的字符串,可以这样做:

>>> [True if "true" in s else False for s in [s for s in li if "true" in s or "false" in s]]
[True, False, True, False, False]

或者:

[True if s=="true" else False for s in [s for s in map(lambda x: x.strip(),li) if s=="true" or s=="false"]]

或者:

di={'true':True, 'false':False}
[di.get(s.strip(), None) for s in li]

【讨论】:

  • 谢谢,谢谢,谢谢!!!
  • 您甚至可以缩短为 [s.strip()=="true" for s in li],因为如果 s 不是 "true" 对于您知道只是 truefalse 字符串的列表,它将返回 False
【解决方案3】:

看起来你几乎已经自己修好了。但我建议您尝试将第 3 行和第 4 行替换为以下内容:

    ConvertidorBool = list(map(lambda ele: ele == " true ", Datos2))
    Datos=[s.Cuerpo for s in Doc if ConvertidorBool == False] # condicion

【讨论】:

  • 感谢您的帮助:3
【解决方案4】:

感谢@dawg 的代码,我能够让我的 str (bool) 成为真正的 bool 我在编写代码时留下了代码,一一删除我开始添加的所有内容:

def percentage (Doc):
    DataBool = [s.DENOUNCIADO for s in Doc]
    Converter_A_Bool = [True if "true" in s else False for s in DataBool]
    Data = [s.Body for s in Doc if Converter_A_Bool [0] == False]
    CC = {Ca: Datos.count (Ca) * 100/1000 for Ca in Data if Ca! = ""}
    Counter = Counter (CC)
    Grouper = Counter.items ()
    Dictionary = dict (Grouper)
    Result = print ("{}". Format (Dictionary))
    return Result

【讨论】:

    猜你喜欢
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多