Comment detail

文字列の均等分割 (Nested Flatten)
文字数数えるのが面倒だったので、スライスでn個飛ばしの文字列を作って、それの文字数を使いました。
オフセット数えるのも、面倒なので再帰任せです。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# encoding: utf-8

sample = u"ゆめよりもはかなき世のなかをなげきわびつゝあかしくらすほどに四月十よひにもなりぬれば木のしたくらがりもてゆく"

def divid(str, n, target_list=[]):
  def get_splitted_str(str, n):
    return str[:len(str[::n])]

  if str:
    s = get_splitted_str(str, n)
    return divid(str[len(s):], n-1, target_list+[s])
  else: return target_list

for i in range(4,7):
  print "divid", i
  l = divid(sample, i)
  for line in l:
    print line
  print ""

Index

Feed

Other

Link

Pathtraq

loading...