Programing Language/Python

Python 리스트 내장 함수 : sorted, startswith

Data-SSung 2020. 7. 30. 22:34
반응형

Python 리스트 내장 함수

sorted, startswith


리스트 내장함수 sorted 편

# 숫자는 오름차순
a = [1,4,3,60,8]
sorted(a)
# 결과 : [1,3,4,8,60]

# 문자열은 앞 글자 기준으로 오름차순
t = ['a', 'ad', 'c','bdvdwe','desg']
sorted(t)
# 결과 : ['a', 'ad', 'bdvdwe', 'c', 'desg']

 

리스트 내장함수 startswith 편

구조 : 문자열.startswith( '찾고 싶은 문자' , beg='탐색 시작하는 점',end='탐색 끝나는 점')

# startswith
# beg : 탐색하는 시점
# end : 끝나는 시점

Text = "this is Data-ssung's blog...wow!!"
text.startswith('Da')
# 결과 : True 

text.startswith('sssung')
# 결과 : False 

text.startswith('Da', beg = 5, end = len(text))
# 결과 : True 

 

sorted와 startswith 활용하기

# 제일 작은 문자열 or 숫자열이 다른 문자열이나 숫자열의 접두사로 있는지 확인 

text = ['11948657','583472', '119']

text = sorted(text)

for i,j in zip(text,text[1:]):
	if i in j:
    	print("False")
    else: print("True")
    
# 결과 : False, True

 

반응형