Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
私は次のようなリストを持っています
[38, 98, 110, 111, 112, 120, 121, 898]
連続した数値を範囲を表すペアにマージするにはどうすればよいですか?
望ましい出力:
['38', '98', '110,112', '120,121', '898']
簡単な解決策:
def ranger(lst): fr, to = lst[0], lst[0] for x in lst[1:]: if x == to+1: to = x else: yield fr, to fr, to = x, x yield fr, to res = [','.join(map(str, sorted(set(x)))) for x in ranger(lst)]