简体   繁体   English

字符串的字母乘以数字的位数

[英]multiplying letter of string by digits of number

I want to multiply letter of string by digits of number.我想将字符串的字母乘以数字的数字。 For example for a word "number" and number "123" output would be "nuummmbeerrr".例如,对于单词“number”和数字“123”,输出将是“nuummmbeerrr”。 How do I create a function that does this?如何创建执行此操作的函数? My code is not usefull, because it doesn't work.我的代码没有用,因为它不起作用。

I have only this我只有这个

def new_word(s):
    b=""
    for i in range(len(s)):
        if i % 2 == 0:
            b = b + s[i] * int(s[i+1])    

return b

for new_word('a3n5z1') output is aaannnnnz .对于 new_word('a3n5z1') 输出是aaannnnnz

Using list comprehension and without itertools :使用列表理解而不使用itertools

number = 123
word = "number"

new_word = "".join([character*n for (n, character) in zip(([int(c) for c in str(number)]*len(str(number)))[0:len(word)], word)])

print(new_word)

# > 'nuummmbeerrr'

What it does (with more details) is the following:它的作用(更多细节)如下:

number = 123
word = "number"

# the first trick is to link each character in the word to the number that we want
# for this, we multiply the number as a string and split it so that we get a list...
# ... with length equal to the length of the word
numbers_to_characters = ([int(c) for c in str(number)]*len(str(number)))[0:len(word)]

print(numbers_to_characters)

# > [1, 2, 3, 1, 2, 3]

# then, we initialize an empty list to contain the repeated characters of the new word
repeated_characters_as_list = []

# we loop over each number in numbers_to_letters and each character in the word
for (n, character) in zip(numbers_to_characters, word):
    repeated_characters_as_list.append(character*n)

print(repeated_characters_as_list)

# > ['n', 'uu', 'mmm', 'b', 'ee', 'rrr']

new_word = "".join(repeated_characters_as_list)

print(new_word)

# > 'nuummmbeerrr'

This will solve your issue, feel free to modify it to fit your needs.这将解决您的问题,请随意修改它以满足您的需求。

from itertools import cycle

numbers = cycle("123")
word = "number"

output = []

for letter in word:
  output += [letter for _ in range(int(next(numbers)))]

string_output = ''.join(output)

EDIT:编辑:

Since you're a beginner This will be easier to understand for you, even though I suggest reading up on the itertools module since its the right tool for this kind of stuff.由于您是初学者,这对您来说会更容易理解,尽管我建议阅读itertools模块,因为它是处理此类内容的正确工具。

number = "123"
word = "number"


output = []
i = 0

for letter in word:
  if(i == len(number)):
    i = 0
  output += [letter for _ in range(int(number[i]))]
  i += 1

string_output = ''.join(output)
print(string_output)

you can use zip to match each digit to its respective char in the word (using itertools.cycle for the case the word is longer), then just multiply the char by that digit, and finally join to a single string.您可以使用zip将每个数字与其相应的字符匹配(使用itertools.cycle用于单词较长的情况),然后将字符乘以该数字,最后加入单个字符串。

try this:尝试这个:

from itertools import cycle

word = "number"
number = 123

number_digits = [int(d) for d in str(number)]

result = "".join(letter*num for letter,num in zip(word,cycle(number_digits)))
print(result)

Output:输出:

nuummmbeerrr

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

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