pimgeek / play

各种好玩的
0 stars 0 forks source link

python map 的同时处理科学计数的数值 #7

Open pimgeek opened 6 years ago

pimgeek commented 6 years ago
import decimal

arr = [[1,' a ', 3.0 ],[' 4?' ,5.112321E15, None]]

def isfloat(val):
    try:
        float(val)
        return True
    except:
        return False

def isint(val):
    try:
        int(val)
        return True
    except:
        return False

def mystr(val):
    if (isfloat(val)):
        return val
    else:
        return str(val)

def mystrip(val):
    if (isint(val)):
        return val
    else:
        return val.strip(' ?')

new_arr = []
for row in arr:
    tmp1 = list(map(mystr, row))
    tmp2 = list(map(mystrip, tmp1))
    new_arr.append(tmp2)

print(new_arr)