« MeadowからGRINEditを操作する |Main| 執筆日記 »

« 1行でテトリス | Python | Pythonのexecfileにlocals()を渡した際の挙動 »

Re: 先頭から2文字ずつ取る

Pythonのジェネレータで四角いらせんを書くのに反応した|Pythonでなんか作ってみるの隣のエントリー先頭から2文字ずつ取る|Pythonでなんか作ってみるに反応してみます。

これでどうでしょ?

>>> def foo(a, b, *rest):
	c = "0x%c%c" % (a, b)
	if not rest:
		return [c]
	else:
		result = foo(*rest)
		result.insert(0, c)
		return result

	
>>> foo(*"123456")
['0x12', '0x34', '0x56']

ジェネレータが使えるならこっちの方がいいかも。

>>> def foo(a, b, *rest):
	yield "0x%c%c" % (a, b)
	if rest:
		for result in foo(*rest):
			yield result

>>> for result in foo(*"123456"):
	print result

	
0x12
0x34
0x56

トラックバック(Trackback)

Trackback URL: http://www.nishiohirokazu.org/mt/mt-tb.cgi/438

ご意見・ご感想をお送りください(フィードバック)

(フィードバックはメールで送信され、基本的に表示されませんが、内容によっては公開させていただくこともございます。ご了承ください。Your comment doesn't appear the page immediately. If the comment has value to other people, it will be put on the page or subsequent entries. Thank you.)

上の情報は、いずれも未記入でかまいません。 All of above questions are optional.