반응형
리스트와 딕셔너리
아래와 같이. sort를 사용하여 정렬해 줄 수 있다
a_list = [1,5,6,4,9,2,8,3,7]
a_list.sort()
print(a_list)
실행 결과 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
반대로 정렬하고 싶다면???
a_list = [1,5,6,4,9,2,8,3,7]
a_list.sort(reverse=True)
print(a_list)
실행 결과 = [9, 8, 7, 6, 5, 4, 3, 2, 1]
배열속에서 값이 있는지 찾기
a_list = [1,5,6,4,9,2,8,3,7]
result = (99 in a_list)
print(result)
실행 결과 = False
99가 a_list에 없기 때문에 False가 출력된다
만약 99 가 있다면 True가 출력된다
배열과 딕셔너리의 조합
아래의 배열에서 smith 의 science 점수를 출력하자
people = [
{'name': 'bob', 'age': 20, 'score':{'math':90,'science':70}},
{'name': 'carry', 'age': 38, 'score':{'math':40,'science':72}},
{'name': 'smith', 'age': 28, 'score':{'math':80,'science':90}},
{'name': 'john', 'age': 34, 'score':{'math':75,'science':100}}
]
정답은 아래
people = [
{'name': 'bob', 'age': 20, 'score':{'math':90,'science':70}},
{'name': 'carry', 'age': 38, 'score':{'math':40,'science':72}},
{'name': 'smith', 'age': 28, 'score':{'math':80,'science':90}},
{'name': 'john', 'age': 34, 'score':{'math':75,'science':100}}
]
print(people[2]['score']['science'])
people 배열에서 2번째 인덱스 값을 가져오고 그 안에서 score의 인덱스 값을 가져오고 그 안에서 science의 값을 가져온다
즉
people[2] = {'name': 'smith', 'age': 28, 'score': {'math': 80, 'science': 90}}
people[2]['score'] = {'math': 80, 'science': 90}
people[2]['score']['science'] = 90
타고 타고 들어가 마지 스코프와 같은 개념이라고 보면 된다
반응형
'Python' 카테고리의 다른 글
로그인 기능 세션 (0) | 2022.12.06 |
---|---|
Python 파이썬 map, filter, lambda (0) | 2022.11.21 |
Python 파이썬 조건문과 반복문 (0) | 2022.11.21 |
Python f-string 과 예외처리 (1) | 2022.11.21 |
Python 문자열 자르기 (0) | 2022.11.21 |
댓글