日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Split Pairs

發(fā)布時(shí)間:2023/12/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Split Pairs 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問(wèn)題描述:

Split the string into pairs of two characters. If the string contains an odd number of characters, then the missing second character of the final pair should be replaced with an underscore ('_').

(將一個(gè)字符串兩兩分開(kāi),最后如果剩一個(gè)就加上“_”重新組成一對(duì))

Input:?A string.

Output:?An iterable of strings.

def split_pairs(a):# your code herereturn Noneif __name__ == '__main__':print("Example:")print(list(split_pairs('abcd')))# These "asserts" are used for self-checking and not for an auto-testingassert list(split_pairs('abcd')) == ['ab', 'cd']assert list(split_pairs('abc')) == ['ab', 'c_']assert list(split_pairs('abcdf')) == ['ab', 'cd', 'f_']assert list(split_pairs('a')) == ['a_']assert list(split_pairs('')) == []print("Coding complete? Click 'Check' to earn cool rewards!")

對(duì)我來(lái)說(shuō)這有點(diǎn)難,split不能用,那就只能用索引,最終有了以下比較繁瑣的方法:?

def split_pairs(a):# your code herelst=[]lst_index=0if len(a)%2==0:for i in range(len(a)//2):lst.append(a[lst_index:lst_index+2])lst_index+=2elif len(a)%2==1:for i in range(len(a)//2):lst.append(a[lst_index:lst_index+2])lst_index+=2lst.append(a[-1]+'_')return lst

其他解決方案:

一:對(duì)我上面想法的一個(gè)更優(yōu)版。

def split_pairs(a):# your code hereif a == '':return []if len(a) % 2 == 1:a = a + "_"b = [a[i:i+2] for i in range(0, len(a), 2)]return b

?

二:很簡(jiǎn)單的一行,但是用了zip,一個(gè)對(duì)我來(lái)說(shuō)新的知識(shí)點(diǎn)。

def split_pairs(a):return [ch1+ch2 for ch1,ch2 in zip(a[::2],a[1::2]+'_')]

官方文檔解釋:

zip(*iterables)

創(chuàng)建一個(gè)聚合了來(lái)自每個(gè)可迭代對(duì)象中的元素的迭代器。

返回一個(gè)元組的迭代器,其中的第?i?個(gè)元組包含來(lái)自每個(gè)參數(shù)序列或可迭代對(duì)象的第?i?個(gè)元素。 當(dāng)所輸入可迭代對(duì)象中最短的一個(gè)被耗盡時(shí),迭代器將停止迭代。 當(dāng)只有一個(gè)可迭代對(duì)象參數(shù)時(shí),它將返回一個(gè)單元組的迭代器。 不帶參數(shù)時(shí),它將返回一個(gè)空迭代器。

也就是說(shuō)兩個(gè)列表中的相同索引會(huì)在一起。而當(dāng)上面輸入的一個(gè)偶數(shù)長(zhǎng)度的字符串時(shí),多余的'_'并不會(huì)被組合到一起,這個(gè)真的是很厲害。

三:還有一個(gè)新的庫(kù),textwrap文本自動(dòng)換行與填充

from textwrap import wrapdef split_pairs(a):a = a + '_' if len(a) % 2 else areturn wrap(a, 2)

?textwrap?模塊提供了一些快捷函數(shù),以及可以完成所有工作的類(lèi)?TextWrapper。 如果你只是要對(duì)一兩個(gè)文本字符串進(jìn)行自動(dòng)換行或填充,快捷函數(shù)應(yīng)該就夠用了;否則的話(huà),你應(yīng)該使用?TextWrapper?的實(shí)例來(lái)提高效率。

textwrap.wrap(text,?width=70,?*,?initial_indent="",?subsequent_indent="",?expand_tabs=True,?replace_whitespace=True,?fix_sentence_endings=False,?break_long_words=True,?drop_whitespace=True,?break_on_hyphens=True,?tabsize=8,?max_lines=None)

對(duì)?text?(字符串) 中的單獨(dú)段落自動(dòng)換行以使每行長(zhǎng)度最多為?width?個(gè)字符。 返回由輸出行組成的列表,行尾不帶換行符。

這個(gè)也是處理這個(gè)問(wèn)題很好的方法。

總結(jié)

以上是生活随笔為你收集整理的Split Pairs的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。