例子:
list = [abcc, typpaw, gfssdwww]
expected result = atgbyfcpscpsadwwww
有任何想法嗎?
這是我到目前為止所做的:
def lazy_scribe(sources: list):
result: str = ''
i = 0
while i < len(max(sources, key=len)):
for source in sources:
for char in source:
if i <= len(source):
result = result source[int(i)]
else:
continue
i = 1 / (len(sources))
break
return result
sources = ["python", "java", "golang"]
print(lazy_scribe(sources))
print(len(sources))
結果:“pjgyaoyvlhaaononngn”。我不知道為什么有“y”而不是 t(結果字串中有 7 個字符)
uj5u.com熱心網友回復:
如果我正確理解了問題,這應該可行。
list = ["abcc", "typpaw", "gfssdwww"]
max_len = len(max(list, key=len))
res = ""
char_iterator = 0
while char_iterator < max_len:
for word in list:
if char_iterator < len(word):
res = word[char_iterator]
char_iterator = 1
print(res)
uj5u.com熱心網友回復:
另一種可能的解決方案如下:
l = ['abcc', 'typpaw', 'gfssdwww']
max_len = len(max(l, key=len))
padded_l = list(zip(*[e " " * (max_len - len(e)) for e in l]))
''.join([''.join(e) for e in padded_l]).replace(' ', '')
- 找到串列中最長的字串
- 然后用空格填充串列中的所有字串
- 使用
zip結果串列 - 加入元素并替換空格以獲得所需的結果
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/359206.html
下一篇:陣列中連續數字的總和
