可能重複:
How do you split a list into evenly sized chunks in Python?
What is the most 「pythonic」 way to iterate over a list in chunks?切片字符串轉換成列表
說我有一個字符串
s = '1234567890ABCDEF'
我如何可以切片(或可能分裂是正確的術語?)這個字符串變成一個列表包含g每個包含2個字符的字符串?
desired_result = ['12', '34', '56', '78', '90', 'AB', 'CD', 'EF']
不知道這是否是相關的,但我解析的十六進制字符的字符串最終的結果我需要的是一個字節一個列表,從上面的列表中創建(例如,通過使用int(desired_result[i], 16)
)
我總是在Py 3.x中忘記'fromhex' - 很好的+1 –
TIL它有一個名字,叫做X-Y :) – miku
'bytes.fromhex'很棒!但是有沒有辦法一次轉換4個(和8個)字符?像'words.fromhex'和'long.fromhex'? – Praetorian