[리스트 문제]
salary 변수에 직원들의 연봉을 담은 리스트가 담겨 있다.
salary 변수의 리스트에 대하여 가장 높은 연봉을 삭제하고, 내림차순으로 정렬하시오
# 만단위
>>> salary = [3500,4000,4500,6000,3600,8000,15000,3500,4000,4500,6000,3600]
의문점 ???
>>> del salary[15000]
>>> salary
-> IndexError: list assignment index out of range
>>> salary.remove(15000)
>>> salary
[3500, 4000, 4500, 6000, 3600, 8000, 3500, 4000, 4500, 6000, 3600]
>>> sum (salary)//len(salary)
4654
del / remove 의 차이
함수 (array의 method) = remove () | 예약어 = del |
변수 이름 뒤에 점(.)을 붙이고서 사용 | 예약어 뒤에 한 칸을 띄고서 사용 |
ex) list_numbers.remove(x) (x값 삭제) | ex) del int list[0] (첫번째 요소 삭제) |
del
- del array [인덱스] 형태로 사용
- 함수가 아니라 예약어이다 -> 함수처럼 사용 불가
- 범위 연산자 슬라이싱(:)을 이용하면 해당 범위에 위치한 여러 개의 요소 삭제 가능
>>> num_list = [0, 1, 2, 3, 4, 5]
>>> alph_list = ['a', 'b', 'c', 'd', 'e', 'f']
>>> del num_list[0] # 처음 한개의 요소를 삭제
>>> print(num_list)
[1, 2, 3, 4, 5]
>>> del alph_list[3:] # 여러개의 요소를 삭제
>>> print(alph_list)
['a', 'b', 'c']
array()
- array의 요소를 삭제
- array 안에서 삭제하고자 하는 값이 여러 개여도 첫 번째 값에 대해서만 삭제
- remove 함수를 사용하여 모든 값을 삭제할 때는 for문을 이용할 수도 있음
>>> numbers = [1, 2, 2, 3, 3, 3]
>>> for _ in numbers :
>>> numbers.remove(2)
>>> print(numbers)
[1, 3, 3, 3] # 숫자 2가 없음
[딕셔너리 문제]
회사의 연도별 매출이 딕셔너리 데이터로 되어있다.
key는 연도 ,value는 매출(억단위)이다.
가장 오래된 연도의 매출값을 출력하시오.
>>> sales = {
>>> 2013: 30,
>>> 1999: 40,
>>> 2020: 50,
>>> 2021: 10,
>>> 1998: 13,
>>> 2019: 45,
>>> 2017: 20,
>>> 2003: 23,
>>> 2012: 35,
>>> 2004: 17,
>>> 2011: 29,
>>> 2007: 16,
>>> }
이건 왜 안대는거야...?
>>> sales [min[sales.keys()]]
-> TypeError: 'builtin_function_or_method' object is not subscriptable
sales 옆에 공백이 있어서!!
그리고 추가로 min은 함수이기 때문에 sales[min(sales.keys())] 이런식으로 해줘야한다
함수 실행 | 슬라이싱, 인덱싱, 딕셔너리에 벨류을 가져오기 위해 키를 넣는 경우 |
소괄호 사용 | 대괄호 사용 |
>>> sales [min(sales.keys())]
13
출력값 맞추기
>>> first = ["egg", "salad", "bread", "soup", "canafe"]
>>> second = ["fish", "Lamb", "pork", "beef", "chicken"]
>>> third = ["apple", "banana", "orange", "grape", "mango"]
>>> order = [first, second, third]
# order [0] = first에서 [-2전까지 가져와라]
# second[1::3] 인덱스 1 값에서 세깐씩 건너 뛰며 가져와라
>>> john = [order [0][:-2], second[1::3], third[0]]
>>> print (john)
[['egg', 'salad', 'bread'], ['Lamb', 'chicken'], 'apple']
>>> first = ["egg", "salad", "bread", "soup", "canafe"]
>>> second = ["fish", "Lamb", "pork", "beef", "chicken"]
>>> third = ["apple", "banana", "orange", "grape", "mango"]
>>> order = [first, second, third]
# order [0] = first에서 [-2전까지 가져와라]
# second[1::3] 인덱스 1 값에서 세깐씩 건너 뛰며 가져와라
>>> john = [order [0][:-2], second[1::3], third[0]]
>>> del john [2]
>>> print (john)
# [0] = ['egg', 'salad', 'bread']
# [1] = ['Lamb', 'chicken']
# [2] = 'apple'
[['egg', 'salad', 'bread'], ['Lamb', 'chicken']]
>>> first = ["egg", "salad", "bread", "soup", "canafe"]
>>> second = ["fish", "Lamb", "pork", "beef", "chicken"]
>>> third = ["apple", "banana", "orange", "grape", "mango"]
>>> order = [first, second, third]
>>> john = [order[0][:-2], second[1::3], third[0]]
>>> print (order [2][0:-2])
['apple', 'banana', 'orange']
>>> first = ["egg", "salad", "bread", "soup", "canafe"]
>>> second = ["fish", "Lamb", "pork", "beef", "chicken"]
>>> third = ["apple", "banana", "orange", "grape", "mango"]
>>> order = [first, second, third]
>>> john = [order[0][:-2], second[1::3], third[0]]
>>> del john [2]
# extend method 리스트와 리스트를 합친다 (그냥 연장한다 느낌)
# ([order [2][0:1]])에서 [0:1]이면 1 전 값까지만 출력
>>> john.extend ([order [2][0:1]])
>>> print (john)
[['egg', 'salad', 'bread'], ['Lamb', 'chicken'], ['apple']]