Skip to content

Instantly share code, notes, and snippets.

@developer-sdk
Created April 8, 2017 13:41
Show Gist options
  • Select an option

  • Save developer-sdk/cbc03f7d0437e1eaec82ac5d46c7cbdf to your computer and use it in GitHub Desktop.

Select an option

Save developer-sdk/cbc03f7d0437e1eaec82ac5d46c7cbdf to your computer and use it in GitHub Desktop.
python concat
#!/bin/python
# -*- coding:utf-8 -*-
'''
방법6이 가장 효율적인 방법이나,
메모리 사용량은 방법4가 가장 효율적이고,
문자열에 조작을 해야 하는 경우라면 방법4가 가장 효율적이다.
'''
# 방법1: + 를 이용한 연결
def method1():
out_str = ''
for num in xrange(1, 10):
out_str += `num`
return out_str
# 방법2: MutableString 을 이용한 방법
# UserString은 더이상 지원되지 않는 클래스임
def method2():
from UserString import MutableString
out_str = MutableString()
for num in xrange(1, 10):
out_str += `num`
return out_str
# 방법3: 캐릭터 배열을 이용한 방법
def method3():
from array import array
char_array = array('c')
for num in xrange(1, 10):
char_array.fromstring(`num`)
return char_array.tostring()
# 방법4: 리스트의 join()을 이용한 방법
def method4():
str_list = []
for num in xrange(1, 10):
str_list.append(`num`)
return ''.join(str_list)
# 방법5: 수도 파일을 이용하는 방법
def method5():
from cStringIO import StringIO
file_str = StringIO()
for num in xrange(1, 10):
file_str.write(`num`)
return file_str.getvalue()
# 방법6: 방법4와 복합 리스트를 이용한 방법
def method6():
return ''.join([`num` for num in xrange(1, 10)])
if __name__ == "__main__":
print method4()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment