简体   繁体   English

如何从字符串中删除空格/哈希并将其替换为特定长度的哈希?

[英]How to remove spaces/hashes from a string and replace them with hashes at a particular length?

I would like to change the string of numbers in a way that all extra spaces and hashes will be removed and the output will have a proper string with hashes at the right length. 我想更改数字字符串的方式是,将删除所有多余的空格和哈希,并且输出将具有正确长度的带有哈希的适当字符串。 It must be after every 3rd digit unless the remaining length of the string is 2 or 4. 除非字符串的剩余长度为2或4,否则它必须在每3位数字之后。

The code I tried with is able to give me half of the result, I am not able to write a code where the remaining length of the string is 4 and then divide them into 2 with a hash in between. 我尝试过的代码可以给我一半的结果,但是我无法编写一个代码,其中字符串的剩余长度为4,然后将它们分为2,中间使用一个哈希。 It divides remaining 4 also into 3 and 1. eg - 1234 must be 12-34 not 123-1. 它将剩余的4也分为3和1。例如-1234必须是12-34,而不是123-1。

def solution(s):
    # this will remove all extra spaces and hashes
    number = "".join(s.split()).replace('-','')
    return '-'.join(number[i:i+3] for i in range(0, len(number), 3))


print(solution("00-44  48 5555 8361")) # Output is 004-448-555-583-61
print(solution("0 - 22 1985--324")) # Output is 022-198-532-4 (not expected)

Output must be like below: 输出必须如下所示:

print(solution("00-44  48 5555 8361")) # Expected O/P - 004-448-555-583-61
print(solution("0 - 22 1985--324")) # Expected O/P -  022-198-53-24

Here you go: 干得好:

def solution(s):
    number = "".join(s.split()).replace('-','')
    return formatnumber(number)

def formatnumber(number):
    if len(number) == 0:
        return ""
    if len(number) == 4:
        return number [:2] + "-" + number[2:]
    if len(number) == 2 or len(number) == 3:
        return number
    return number[:3]+ "-" +solution(number[3:])


print(solution("00-44  4 8 5555 8361")) # Output is 004-448-555-583-61
print(solution("0 - 22 1985--324")) # Output is 022-198-53-24 
print(solution("00-44  48 5555 83613")) # Output is 004-448-555-583-613

The reason your code does not work as intended I think is obvious--placing a hyphen between every 3rd character in a greedy fashion like you are doing will leave len(number)%3 (either 0, 1, or 2) characters at the end of the string. 我认为您的代码无法正常工作的原因很明显-像您所做的那样,以贪婪的方式在每个第三个字符之间加上连字符会在len(number)%3 (0、1或2)个字符处留下字符串的结尾。 Based on your requirements, all you need to worry about is the case when there is exactly 1 remaining character: 根据您的要求,您只需要担心剩下的1个字符:

  1. "123-456-789" has zero remaining characters, and it's fine. "123-456-789"剩余字符为零,这很好。
  2. "123-456-789-0" has one remaining character, but should be "123-456-78-90 " "123-456-789-0"剩余一个字符,但应为"123-456-78-90
  3. "123-456-789-01" has two remaining characters, and it's fine. "123-456-789-01"剩余两个字符,这很好。
  4. "123-456-789-012" has three remaining characters, but it's the same pattern as (1.), above, so it's fine. "123-456-789-012"剩余三个字符,但是与上面(1.)的模式相同,所以很好。

This means all you have to do is deal specially with the case when there is one remaining character. 这意味着您要做的就是专门处理剩下的一个字符的情况。 Even better, all you have to do to correct it is swap the third-to-last and second-to-last characters, like so: 甚至更好的是,您只需将倒数第二个和倒数第二个字符交换,就可以更正它,如下所示:

"123-456-789-0"  # this is wrong
#          ^^
#          ||    # swap these two, however, and you get...
"123-456-78-90"  # the correct output

So you can keep your original code and add a simple check at the end to make the swap. 因此,您可以保留原始代码,并在末尾添加简单的检查以进行交换。 Unfortunately, strings are immutable in python, so you have to reconstruct a new string with the swapped characters. 不幸的是,字符串在python中是不可变的,因此您必须使用交换的字符来重建新的字符串。 Doing this the long way: 很长的路要走:

def solution(s):
    # this will remove all extra spaces and hashes
    number = "".join(s.split()).replace('-','')
    dashed = '-'.join(number[i:i+3] for i in range(0, len(number), 3))
    if len(number)%3 == 1:
        dashed = dashed[:-3] + "-" + dashed[-3::2]
    return dashed
def solution(s):
    s.strip()
    number = "".join(s.split()).replace('-','')
    newN=''
    while(len(number)>4):
        newN=newN+number[0:3]+'-'
        number=number[3:]
    if len(number)==4:
        newN=newN+number[0:2]+'-'+number[2:]
    elif len(number)==2 or len(number)==3:
        newN=newN+number
    print(newN)

solution("00-44  48 5555 8361") # Output is 004-448-555-583-61
solution("0 - 22 1985--324") # 022-198-53-24

You can check condition for 2 and 4 manually. 您可以手动检查24条件。

import re
def solution(s):
    number = re.sub('[- ]','',s)
    ans = ''
    if len(number)%3 == 2:
        for i in range(0, len(number)-2,3):
            ans += number[i:i+3]
            ans += '-'
        ans += number[-2:]
    elif len(number)%3 == 1:
        for i in range(0, len(number)-4,3):
            ans += number[i:i+3]
            ans += '-'
        ans += (number[-4:-2] + '-' + number[-2:])
    else:
        for i in range(0, len(number)-4,3):
            ans += number[i:i+3]
            ans += '-'
        ans = ans[:-1] # this is required because we are appending `'-'` after each slice and last slice will also have dash at the end.
    return ans

print(solution("00-44  48 5555 8361")) # Expected O/P - 004-448-555-583-61
print(solution("0 - 22 1985--324")) # Expected O/P -  022-198-53-24   

Output: 输出:

004-448-555-583-61
022-198-53-24

UPD: This is oneliner solution for you. UPD:这是为您提供的oneliner解决方案。

def solution(s):
    ss = re.sub('[- ]','',s)
    l = len(ss)
    r = l - (4 if l%3==1 else l%3)
    return '-'.join(ss[i:i+3] for i in range(0,r,3)) + (('-' + ss[-4:-2]) if l%3 == 1 else '') + (('-' + ss[-2:]) if l%3!=0 else '' )

PS: this code can be reduced by few lines. PS:此代码可以减少几行。 But the main idea is same that you need to handle both 2 and 4 case. 但是主要思想是相同的,您需要同时处理24两种情况。

Another way of solving this is to work with the length of the string. 解决此问题的另一种方法是使用字符串的长度。 If the length - 4 is modulo 3, then format the last 4 characters specifically into -nn-nn format, otherwise treat the overall string as 'chunks of 3 with whatever remains at end of string'. 如果长度-4为模3,则将最后4个字符特别格式化为-nn-nn格式,否则将整个字符串视为“ 3的块,并保留字符串末尾的任何内容”。

from textwrap import wrap

def solution(s):
    output_string = ""
    s = s.replace(' ', '').replace('-', '')
    if (len(s)-4) % 3 == 0:
        end_text = s[-4:][0:2] + '-' + s[-4:][2:]
        output_string = '-'.join(wrap(s[0:-4], 3)) + '-' + end_text
    else:
        output_string = '-'.join(wrap(s, 3)) 
    return output_string

print(solution("00-44  48 5555 8361"))
print(solution("0 - 22 1985--324"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM