vendredi 27 juillet 2018

Not working if/else in one line Python

s_headers=[]
headers=['DRIVER’S NAME', 'LICENSE', 'PH NUMBER', 'DOB', 'HIRE DAY']
for head in headers:
    if isinstance(head, float):
        s_headers.append(str(int(head)))
    else:
        s_headers.append(head)
print(s_headers)

output:-

['DRIVER’S NAME', 'LICENSE', 'PH NUMBER', 'DOB', 'HIRE DAY']

when i am trying to reduce this code using this,

s_headers.append(str(int(head) if (isinstance(head, float)) else head) for head in headers)
print(s_headers)

output:-

[ at 0x7f2aeec50780>]

Aucun commentaire:

Enregistrer un commentaire