删除字符串中的字符,然后使用字典进行键匹配

2024-04-25 06:29:32 发布

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

我想写一个函数,通过使用map和reduce将字符串转换成float。(此处没有int()。 以下是我目前的代码:

from functools import reduce

def str2float(s):
    def char2number(s):
        s = s.replace('.', '')
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] #here i try to get pure numbers 

    def find_digt(s):
        return (len(s) - s.find('.') - 1) if ',' in s else 0

    return reduce(lambda x, y: (10 * x + y), (map(char2number, s))) / pow(10, find_digt(s))


print(str2float('1456.124'))

在这之后,我得到一个错误:

return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[sn]  # [s] is the [key]
KeyError: ''

这意味着“”不在dict中。我做了一些测试,比如:

s = '1234.456'
s = s.replace(',', '')
print('' in s) #True

所以现在的问题是,一次

s = s.replace('.', '')

“.”替换为“”,但未清除字符串中的“.”。你知道吗

我想知道这是怎么回事。什么是清除字符串中字符的正确方法,因为它是不可变的。你知道吗


Tags: 函数字符串inmapreducereturndeffind
3条回答

您的想法是正确的,只需在传递给map函数之前替换.中的s

map(char2number, s.replace('.',''))

注意:find_digt()中有一个输入错误,应该是:if '.' in s...
如果您还想处理逗号,请添加一个额外的replace来消除它们:

stripped_s = s.replace('.','').replace(',','')
return reduce(lambda x, y: (10 * x + y), (map(char2number, stripped_s))) / pow(10, find_digt(s))

print(str2float('1,456.124'))
1456.124

完整代码:

def str2float(s):
    def char2number(s):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]  

    def find_digt(s):
        return (len(s) - s.find('.') - 1) if '.' in s else 0

    stripped_s = s.replace('.','').replace(',','')
    return reduce(lambda x, y: (10 * x + y), (map(char2number, stripped_s))) / pow(10, find_digt(s))

    print(str2float('1,456.124'))
    1456.124

我添加了一个无关紧要的跟踪语句:

def char2number(s):
    print ("ENTER", s)
    s = s.replace('.', '')
    return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]

输出:

ENTER 1
ENTER 4
ENTER 5
ENTER 6
ENTER .
Traceback (most recent call last):
  File "so.py", line 15, in <module>
    print(str2float('1456.124'))
  File "so.py", line 12, in str2float
    return reduce(lambda x, y: (10 * x + y), (map(char2number, s))) / pow(10, find_digt(s))
  File "so.py", line 7, in char2number
    return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] #here i try to get pure numbers 
KeyError: ''

将单个字符“1234.456”传递给char2number。当您传递点时,replace会清除它,并且您将得到一个空字符串。你知道吗

我不知道你打算怎么做。您似乎希望将整个字符串发送到支持函数。但是,map处理iterable中的元素序列。你知道吗

至于改变一个字符串,你不能,真的:你做了我们所能做的,把replace的输出赋回到原来的字符串。您遇到的问题很简单,它在代码中的某个点对单个字符进行操作。你知道吗

问题是,在未更改的输入字符串上使用map进行迭代,因为只删除了char2number函数中的.。在功能之外,没有进行更换。你知道吗

我建议你尽快找到位置并替换它:

from functools import reduce

def str2float(s):
    pos_decimal_point = (len(s) - s.find('.') - 1) if '.' in s else 0
    s = s.replace('.', '')

    def char2number(s):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] #here i try to get pure numbers 

    return reduce(lambda x, y: (10 * x + y), 
                  (map(char2number, s))) / pow(10, pos_decimal_point)


print(str2float('1456.124'))

相关问题 更多 >