본문 바로가기
코딩테스트/프로그래머스

[프로그래머스] LV1 데이터분석 | 파이썬

by 애플파ol 2025. 5. 7.

Solution :

def solution(data, ext, val_ext, sort_by):

    if ext =='code':
        ext=0
    elif ext == 'date':
        ext=1
    elif ext == 'maximum':
        ext=2
    elif ext == 'remain':
        ext=3
    
    # val_ext 를 기준으로 추출하고
    new=[]
    for i in range(len(data)):
        if val_ext > data[i][ext]: # val_ext보다 작은거만 추가함. 
            new.append(data[i])
            
    # print(new)
    
    
    # sort_by 로 정렬 함.
    if sort_by =='code':
        sort_t=0
    elif sort_by == 'date':
        sort_t=1
    elif sort_by == 'maximum':
        sort_t=2
    elif sort_by == 'remain':
        sort_t=3
    new.sort(key = lambda x:x[sort_t],reverse=False) #오름차순
    
    
    return new

 

 

 

Skills :

  1. 문자열의 인덱스를 찾고싶다 : index함수
    (index 함수 (리스트, 튜플, 문자열) 에서만 사용 가능)
list = ["code", "date", "maximum", "remain"]
ext_idx = list.index(ext)
sort_idx = list.index(sort_by)