【问题标题】:string methods in python [closed]python中的字符串方法[关闭]
【发布时间】:2013-10-06 19:03:24
【问题描述】:

我需要有关如何在字符串上定义和测试三个函数的帮助。遵循这些准则。这是我周三考试的复习,​​我真的很想有正确的解决方案,因为我的都是语法错误。

我需要按照下面列出的要求为所有三个示例提供代码。

不使用任何字符串方法仅len函数和字符串操作+*、索引切片和==用于比较字符串或字符。

repl 函数中,使用累加器模式构建新字符串。

示例

  1. ends 函数接受一个字符串作为参数;如果字符串有两个或多个字符,则返回一个由给定字符串的第一个和最后一个字符组成的字符串;否则,它返回给定的字符串。

    >>> ends("ab")
    'ab'
    >>> ends("abc")
    'ac'
    >>> ends("a long sentence")
    'ae'
    >>> ends("")
    ''
    >>> ends("*")
    '*'
    
  2. butends 函数接受一个字符串参数;如果字符串有两个或更多字符,则返回一个字符串,该字符串由字符串的第一个和最后一个字符组成;否则,它返回给定的字符串。

    >>> butends("abcde")
    'bcd'
    >>> butends("abc")
    'b'
    >>> butends("a long sentence")
    ' long sentenc'
    >>> butends("")
    ''
    >>> butends("a")
    'a'
    
  3. repl 函数接受三个参数:

    • old 是单个字符;
    • new 为 0 个或多个字符的字符串;
    • s 是任意字符串。

    我知道它会返回一个新字符串,该字符串是通过将 s 中每次出现的 old 替换为 new 形成的。

    >>> repl('a', 'A', 'fast faces react snappily')
    'fAst fAces reAct snAppily'
    >>> repl('*', '+++', 'a*b = c*d')
    'a+++b = c+++d'
    >>> repl(' ', '\n', 'Practice every day.')
    'Practice\nevery\nday.'
    >>> print(repl(' ', '\n', 'Practice every day.'))
    Practice
    every
    day.
    >>> repl(",", ":", "a,b,cde,fghi")
    'a:b:cde:fghi'
    

到目前为止,我对第 3 部分的了解是:

 def repl(old, new, s):
     newStr = ""
     for ch in s:
         if ch != old:
             newStr = newStr + ch
         else:
             newStr = newStr + new
     return newStr

上面列出的代码没有替换正确的字符我不确定我哪里出错了。

【问题讨论】:

  • 我相信会有更多人这样说,但你有什么尝试?
  • “[…] 真的很想有正确的解决方案,因为我的都是语法错误。” – 向我们展示你的解决方案和什么不起作用,我们会尽力帮助您解决它们。您可以从纠正自己的想法中学到更多,而不仅仅是获得完整的功能。
  • 让我印象深刻的是,在这些限制条件下,第三个问题比前两个问题要难得多。
  • 前两个可以通过索引切片轻松完成。对于第三个,请使用您的提示:)。

标签: python string indexing


【解决方案1】:

这是三个功能的一种可能的解决方案。请注意,正如我在上面的 cmets 中提到的,如果您向我们展示您尝试过的方法以及存在的问题,您会学到更多。

def ends (s):
    if len(s) > 2:
        return s[0] + s[-1]
    else:
        return s

def butends (s):
    if len(s) > 2:
        return s[1:-1]
    else:
        return s

def repl (find, replacement, s):
    newString = ''
    for c in s:
        if c == find:
            newString += replacement
        else:
            newString += c
    return newString

【讨论】:

  • 如果允许str.join''.join(replacement if c == find else c for c in s)
  • @StevenRumbalski 我认为str.join 是一个“字符串方法” ;)
  • 是的——这就是我限定我的陈述的原因。但是,str.join 在概念上确实与其他字符串方法不同。
  • 对于第三个示例,我认为它的底部需要有一个 return 语句,但不确定究竟需要返回什么
  • @HTCOne 没错,谢谢!更正了!
【解决方案2】:
  1. 如果你可以使用len()和slicing,最好是简单的抓取 第一个和最后一个字符并返回。

    def ends(input):
        if len(input) < 2:
            return input
        else:
            return input[0] + input[-1]
    
  2. 你几乎可以在这里做同样的事情:

    def butends(input):
        if len(input) < 2:
            return input
        else:
            return input[1:-1]
    
  3. 对于这个,Python 中有一个名为replace 的函数,但我是 不知道能不能用。

    def repl(old, new, input):
        return input.replace(old, new)
    

如果不能,那么只需循环输入并在每个字符与新字符匹配时替换它。

【讨论】:

  • 然后简单地循环输入并在每个字符与新字符匹配时替换它。 请注意,这不能用字符串完成,因为它们是不可变的。因此 OP 必须将字符串转换为列表才能执行此操作。或者新建一个字符串
  • ^ Smac89 是正确的,你需要创建一个新的字符串,当字符不匹配的时候复制过来,匹配的时候替换掉。
【解决方案3】:

1)

def ends(s):
    if len(s)<=2: return s
    return s[0]+s[-1]

2)

def butends(s):
    if len(s)<=2: return s
    return s[1:-1]

3)

def repl(s,old,new):
    return s.replace(old,new)

【讨论】:

  • 不使用任何字符串方法(第三个)
【解决方案4】:

我喜欢编程作业:

def ends (s): return s [0] + s [-1] if len (s) > 1 else s
def butends (s): return s [1:-1] if len (s) > 1 else s
def repl (a, b, c, acc = ''): return acc if not c else repl (a, b, c [1:], acc + (b if c [0] == a else c [0] ) )

不确定什么是“累加器模式”,因此我使用了一个递归函数和函数式编程中已知的累加器。

【讨论】:

  • endsbutends'a' 生成错误的解决方案。另外:repl 的递归解决方案?这可能有点过头了^^(我还是喜欢它:P)
  • 也不确定在这种情况下累加器模式应该是什么,但在 python 中实现它的理智(阅读:非赋值)方法将简单地使用replace :)
  • @poke 谢谢,我换了butends。但是ends('a') 产生a 符合规范,还是不是?
  • @poke 如果长度小于2,则原样返回原字符串。
  • @Hyperboreus 哦,是的,你是对的。我在看东西……
猜你喜欢
  • 2014-07-10
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
相关资源
最近更新 更多