I need a list of bool named REPORTED [" true "," false "," true "," false "," false "]
convert it to a type bool: [true, false, true]
and then put it in a condition where I execute the following code that tries to get the average percentage of the total depending on 2 list in this case s.Body [Physical appearance]
and s.Reported [true: reported]
that is: calculate how many people with physical aspects [fat, muscular, skinny]
were not Reported [false]
and get the percentage of each aspect
My problem is that I don't need "true" or "false" (in str) if not in bool and I don't know how to put the condition to identify the bool that I think I converted
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
CodePudding user response:
You can use ast.literal_eval
:
>>> import ast
>>> ast.literal_eval(" true ".strip().title())
True
>>> ast.literal_eval(" false ".strip().title())
False
It looks like you just need to strip any surrounding whitespace in your values and capitalize them properly to create strings that contain exactly a bool
literal.
However, literal_eval
doesn't care about types, so if your list could contain non-Boolean literals like "5"
or "'foo'"
, then literal_eval
will happily create 5
and 'foo'
for you. In that case, explicit comparisons are probably a better idea (and more efficient, too).
CodePudding user response:
If you know the data are all [" true "," false "," true "," false "," false "]
you can do:
li=[" true "," false "," true "," false "," false "]
>>> [True if "true" in s else False for s in li]
[True, False, True, False, False]
Alternatively:
>>> [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...
If there are unrelated strings in the source list, you can do:
>>> [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]
Or:
[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"]]
Or:
di={'true':True, 'false':False}
[di.get(s.strip(), None) for s in li]
CodePudding user response:
It looks like you've almost fixed it yourself. But I suggest you try to replace lines 3 and 4 with following:
ConvertidorBool = list(map(lambda ele: ele == " true ", Datos2))
Datos=[s.Cuerpo for s in Doc if ConvertidorBool == False] # condicion
CodePudding user response:
thanks to @dawg for his code I was able to make my str (bool) become the true bool I leave the code as I wrote it delete everything 1 by 1 I started adding:
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