迭代不适用于双字符吗?

huangapple go评论84阅读模式
英文:

Iteration does not work for double characters?

问题

"Beginner here. I'm having problems with this task: accum("hello") should return "H-Ee-Lll-Llll-Ooooo". But what I get with my code is "H-Ee-Lll- Lll -Ooooo". It doesn't work for double characters. Is this because the iteration variable in "for i in s" "skips" over double "i's" or something? And do you have an idea how I can fix this? I'm not striving for elegant code or something, my goal atm is to try and make easily readable lines for myself 迭代不适用于双字符吗?
Thank you!
(Sorry if this is something basic, I didn't really know what to search for!)"

def accum(s):
    s_list = []
    s = [ele for ele in s] 
    for i in s:
        sum_ind = ((s.index(i)) + 1) * i
        s_list.append(sum_ind)
    s_list = [e.capitalize() for e in s_list]
    s_list = '-'.join(s_list)
    return s_list

请注意,上面的代码段并没有修改,仅提供了原始的代码部分。

英文:

Beginner here. I'm having problems with this task: accum("hello") should return "H-Ee-Lll-Llll-Ooooo". But what I get with my code is "H-Ee-Lll- Lll -Ooooo". It doesn't work for double characters. Is this because the iteration variable in "for i in s" "skips" over double "i's" or something? And do you have an idea how I can fix this? I'm not striving for elegant code or something, my goal atm is to try and make easily readable lines for myself 迭代不适用于双字符吗?
Thank you!
(Sorry if this is something basic, I didn't really know what to search for!)

def accum(s):
        s_list = []
        s = [ele for ele in s] 
        for i in s:
            sum_ind = ((s.index(i)) + 1) * i
            s_list.append(sum_ind)
        s_list = [e.capitalize() for e in s_list]
        s_list = '-'.join(s_list)
        return s_list

答案1

得分: 3

这是一种方法:

def accum(stri):
    p = []
    for i, s in enumerate(stri, 1):
        p.append((s*i).capitalize())
    return '-'.join(p)

accum('hello')
'H-Ee-Lll-Llll-Ooooo'

快速了解一下:enumerate

英文:

Here's a way to do:

def accum(stri):
    p = []
    for i, s in enumerate(stri, 1):
        p.append((s*i).capitalize())
    return '-'.join(p)

accum('hello')
'H-Ee-Lll-Llll-Ooooo'

Take a quick read about: enumerate

答案2

得分: 0

这是一种方法:

def accum(s):
    return "-".join((c * i).capitalize() for i, c in enumerate(s, 1))

生成的结果是:

'H-Ee-Lll-Llll-Ooooo'
英文:

Here is one way:

def accum(s):
    return "-".join( (c*i).capitalize() for i,c in enumerate(s,1) )

yields:

'H-Ee-Lll-Llll-Ooooo'

答案3

得分: 0

我认为你可以使用 enumerate 轻松解决这个问题:

def accum(s):
    r = []
    for i, letter in enumerate(s):
        r.append(letter.upper() + (letter*i).lower())
    return '-'.join(r)
英文:

I think you could solve this easily with enumerate:

def accum(s):
    r = []
    for i, letter in enumerate(s):
        r.append(letter.upper() + (letter*i).lower())
    return '-'.join(r)

答案4

得分: 0

正如许多评论中提到的,我们可以在这里简要解释 enumerate 的工作原理。
根据您的要求,给定字符串中的一个字母,您要找到它的索引(位置)。然后,您首先将该字母变成大写,并与小写字母连接 index 次。

因此,您需要一个计数器来跟踪字母的位置,这样我们可以像这样做(一个虚拟的、慢速的示例):

def accum(word):
    ans = ""
    for index in range(len(word)):
        letter = word[index]
        ans += letter.upper() + index * letter + "-"
    else:
        word = word[:-1]  # 去除末尾的 '-'
    return word

但是这个函数非常慢。因为它使用了简单的字符串相加,而不是其他适当的方法。
这就是为什么人们建议您使用 enumerate。简而言之,它跟踪您的索引。

for index, name in enumerate(["John", "Lex", "Vui"]):
    print(name, "is found at", index)

就是这样!
{我没有提供您想要的答案,因为几乎每个人都提供了他们所能提供的最佳答案,我的目的是向您解释如何使用 enumerate 和其他慢速方法来解决您的问题}

英文:

As mentioned in many comments, we can give a short explanation of working of enumerate here.
As per your requirement, given a letter from a string, you find its index (position). Then you first make the caps of letter and glue it with index-times small letters.

So you need a counter which keeps track of the position of letter, so we can do something like this (A DUMMY, SLOW EXAMPLE):

def accum(word):
    ans = ""
    for index in range(len(word)):
        letter = word[index]
        ans += letter.upper() + index*letter + "-"
    else:
        word = word[:-1] #Remove trailing '-'
    return word

BUT THIS FUNCTION IS EXTREMELY SLOW. BECAUSE IT USES SIMPLE STRING ADDITION AND NOT OTHER PROPER METHOD.
That's why people ask you to use enumerate. In short it keeps track of your indices.

for index, name in enumerate(["John", "Lex", "Vui"]):
    print(name, "is found at", index)

That's it !
{Im not writing the answer you wanted, as almost everyone provided the best answer, they could, my aim was to explain you the use of enumerate and other slow methods for your problem}

huangapple
  • 本文由 发表于 2020年1月4日 01:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定