본문 바로가기
  • Top Genius in the world
알고리듬

[알고리듬] #29 리스트 튜플 레인쥐 메소드

by Mr.Algo 2024. 3. 31.
728x90
반응형

파이썬 실습창을 열 수 있습니다.실습창 열기

 

You will face many defeats in life, but never let yourself be defeated.
 – Maya Angelou
인생에서 많은 패배에 직면하겠지만 결코 패배하지 말라. (마야 안젤루)

 

1. 리스트 메소드(list method)

a=[1,2,3]        # a는 list type object
print(dir(a))    # list의 모든 method 출력

위의 코드는 리스트의 모든 메소드를 출력합니다(더보기).

우리는 밑줄(underscore)없는 메소드에 대하여 공부합니다.

더보기
더보기

['__add__', '__contains__', '__delitem__', '__eq__', 
'__format__', '__ge__', '__get__', '__getattribute__', 
'__getitem__', '__gt__', '__hash__', '__init__', 
'__iter__', '__le__', '__len__', '__lt__', '__mul__',
'__ne__', '__new__', '__repr__', '__rmul__', '__setattr__', 
'__setitem__', '__str__', 'append', 'count', 'extend',
'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

 

다음 표의 example을 실습하세요.

no item form example output description
1 list(s) s=(1,3,5)
a=list(s)
print(type(s))
print(type(a))
<class 'tuple'>
<class 'list'>
s를 list 로 바꾼다.
(형변환)
2 s.count(x) s=[1,3,3,5]
a=s.count(3)
print(a)
2 x의 출현 개수
3 s.index(x[,start[,stop]]) s=[1,3,3,5]
a=s.index(3,2)
print(a)
2

s[i]==x인 가장 작은 i
startend는 시작, 종료
x가 없으면 ValueError 발생
4 s,append(x) s=[1,3,5]
s.append(7)
print(s)
[1, 3, 5, 7] s의 끝에 원소 추가
5 s.extend(t) s=[1,3,5]
t=[2,4,6]
s.extend(t)
print(s)
[1, 3, 5, 2, 4, 6] s의 끝에 list t 추가
6 s.insert(i,x) s=[1,3,3,5]
s.insert(3,4)
print(s)
[1, 3, 3, 4, 5] index i위치에 x를 삽입
7 s.remove(x) s=[1,3,3,5]
s.remove(3)
print(s)
[1, 3, 5] 왼쪽부터 x를 찾아서 제거
index 방식 삭제는,.
del s[i:j [:stride]]
  s.cleaar() s=[1,3,3,5]
s.clear()
print(s)
[] s의 모든 item 제거
remove는 값,
del,pop
은 인덱스 사용
8 s.pop(i) s=[1,3,3,5]
a=s.pop(2)
print(a)
3 index i의 원소 제거하고 값 반환
i를 생략하면 마지막 값 제거
9 s.reverse() s=[1,3,3,5]
s.reverse()
print(s)
[5, 3, 3, 1]

s의 item 역순.
10 s.sort(key [,reverse]]) s=[1,5,3,7,5]
s.sort()
print(s)
[1, 3, 5, 5, 7] s의 item 정렬(sort)
item은 같은 type이어야 한다.
제자리(in-place)연산이다.
s=['one','TWO','Three']
s.sort(key=str.lower,
reverse=True)

print(s)
['TWO', 'Three', 'one'] key: 소문자로 인식을 지시
reverse: 역순 생성 지시
key는 사용 방법이 많다.
a='You need py.'.split()
a.sort(key=str.lower,
reverse=True)

print(a)
['You', 'py.', 'need'] 문자열을 단어로 나누어
sort한 예이다.
in-place 연산이다.
sorted(object
[,key [,reverse]])
s=['one','TWO','Three']
a=sorted(s,
key=str.lower,
reverse=True)


print(a)
['TWO', 'Three', 'one'] sorted는 함수
s의 item을 sort하여
다른 변수에 할당한다
.

s의 내용은 변하지 않는다.
out-place 연산이다.

 

  • 'in-place 연산'은 자기자신이 변하는 연산입니다.
  • out-place 연산은 자신은 변하지 않고, 변화 결과를 반환한다. 

 

2. 튜플 메소드(tuple method)

no item example output description
1 s.count(x) s=1, 2, 3, 2, 4, 2, 5
a=s.count(2)
print(a)
3 s에는 23개있다.
2 s.index(x[,start[,stop]]) s=1, 2, 3, 2, 4, 2, 5
a=s.index(2,4)
print(a)
5

index 4 이상에서
처음 나오는 2
index 5이다.

 

 

3. 레인쥐 메소드(range method)

no item example output description
1 s.count(x) s=range(3,9,2)
a=s.count(7)
print(a)
1 s3, 5, 7의 값이 있으므로
71회 출현한다.
2 s.index(x[,start[,stop]]) s=range(3,9,2)
a=s.index(7)
print(a)
2

7index 2에 있다.
3 s.start
s.stop
s.step
s=range(3,9,2)
a=s.start
b=s.stop
c=s.step
print(a,b,c)
3 9 2 s의 시작, 마침, 증가 값은
각각 3, 9, 2 이다.

 

 

4.  스스로 확인하기

다음 프로그램들에 대한 출력을 예상하고 확인하여 보세요.

no program output no program output
1 s=[1,3,3,5]
a=s.count(3)
print(a)
  6 s=[1,3,3,5]
s.append(2)
print(s)
 
2 s=[1,3,3,5]
a=s.index(3)
print(a)
  7 s=[1,3,3,5]
a=s.pop()
print(s)
print(a
 
3 s=[1,3,3,5]
s.insert(3,4)
print(s)
  8 s=[1,3,3,5]
s.reverse()
print(s)
 
4 s=[1,5,3,7,5]
s.sort()
print(s)
  9 a='orange'
b=list(a)
c=sorted(b)
print(a)
print(b)
print(c)
 
5 a='You need py.'.split()
a.sort(key=str.lower,reverse=True)
print(a)
10 name='banana'
s=f'{name:^10}'
print(s)
 

 

더보기
더보기

 

no program output no program output
1 s=[1,3,3,5]
a=s.count(3)
print(a)

2

3의 개수
6 s=[1,3,3,5]
s.append(2)
print(s)  <-- s 출력
[1, 3, 3, 5, 2]
끝에 2 추가
2 s=[1,3,3,5]
a=s.index(3)
print(a)
1
최초 3이 있는
인덱스
7 s=[1,3,3,5]
a=s.pop()
print(s)
print(a
[1, 3, 3]
5
s의 마지막 제거
제가값은 a에
3 s=[1,3,3,5]
s.insert(3,4)
print(s)
[1, 3, 3, 4, 5]
인덱스 3에

4를 삽입
8 s=[1,3,3,5]
s.reverse()
print(s)
[5, 3, 3, 1]
거꾸로 

4 s=[1,5,3,7,5]
s.sort()
print(s)
[1, 3, 5, 5, 7]
오름차순 정렬
9 a='orange'
b=list(a)
c=sorted(b)
print(a)
print(b)
print(c)
orange
['o', 'r', 'a', 'n', 'g', 'e']
['a', 'e', 'g', 'n', 'o', 'r']
5 a='You need py.'.split()
a.sort(key=str.lower,
reverse=True)

print(a)

['you', 'py.', 'need']
a를 공백으로 분할

소문자,
내림차순 정렬
10 name='banana'
s=f'{name:^10}'
print(s)
  banana  
10칸

가운데 정렬

 

 

수고하셨습니다.

오늘 끝.

728x90
반응형