我有字符數組,看起來像這樣:拆分的數組子陣
chars = ["x", "o", "o", "x", "x", "o", "o", "x", "x", "x", "o", "o"]
我需要連續字符的數量和字符的索引。它應該是這樣的:
[
{ index: 0, length: 1 }, # "x"
{ index: 1, length: 2 }, # "o", "o"
{ index: 3, length: 2 }, # "x", "x"
{ index: 5, length: 2 }, # "o", "o"
{ index: 7, length: 3 }, # "x", "x", "x"
{ index: 10, length: 2 } # "o", "o"
]
是否有一個簡單的方法來實現這一目標?
請顯示您嘗試的內容。 – lurker
什麼是'x's和'o's?它們是局部變量嗎?或者是其他東西? – sawa
這些只是字符'x'和'o' –