알쏭달쏭 공부한거 쓰기

HW11-파일 처리, map,filter 본문

파이썬 -23여름학기(ㄱㅎㅈ)

HW11-파일 처리, map,filter

elec_cy 2023. 7. 11. 19:19

hw11.py
0.00MB

13일차 실습 문제 
https://colab.research.google.com/drive/1uococtEI2292KMzDQjtTwl6EaXsy5X6v?usp=sharing

 

Day_13.ipynb

Colaboratory notebook

colab.research.google.com

#3

 

def a():
    with open('sales.txt',encoding='utf8') as f:
        i=[]
        for x in f:
            print(type(x))
            i.append(int(x))
        print(i)
    with open('summary.txt','w') as f:
        f.write(f'총매출={sum(i)}\n')
        f.write(f'평균 일매출 ={sum(i)/len(i):.1f}')
a()

4)

def a():
    with open('alpha_tst.txt',encoding='utf8') as f:
        a=[chr(x) for x in range(97,123)]
        print(a)
        b=[]
        for i in range(26):
            b.append(0)
        print(b)
        for i in f:
            for x in range(len(a)):
                n=i.count(a[x])
                b[x]+=n
        print(b)        
        
a()

 

 

12일차 실습 문제
https://colab.research.google.com/drive/1Jzdka6KRChHZOut2bapC3nFYa48-3DyB?usp=sharing

 

Day_12.ipynb

Colaboratory notebook

colab.research.google.com

10.3

def a():
    persons = [('GilDong', 'Hong', 27), ('SunSin', 'Lee', 46), ('YuSin', 'Gim', 34)]  
    print(sorted(persons,key=lambda x:x[2]))

a()
def a():
    persons = [('GilDong', 'Hong', 27), ('SunSin', 'Lee', 46), ('YuSin', 'Gim', 34)]  
    print(sorted(persons,key=lambda x:x[1]))

a()

 

10.14

def a():
    fruits = {'Apple': '사과', 'Strawberry': '딸기', 'Peach': '복숭아', 'Grape': '포도'}
    a=[]
    for i in fruits:
        a.append(f'{i}={fruits[i]}')
    print(a)
    b=list(map(lambda x:f'{x}={fruits[i]}',fruits))
    print(b)
    print(list(fruits.items()))
a()

10.15

def a():
    fruits_list = ['Apple=사과', 'Strawberry=딸기', 'Peach=복숭아', 'Grape=포도']
    
    c=[tuple(x.split('=')) for x in fruits_list]
    print(dict(c))
    
    
    fruits={}
    for x in fruits_list:
        E,K=x.split('=')
        fruits[E]=K
    print(fruits)  
    
        

a()

10.24

def a():
    a = [(2, 5), (1, 4), (4, 7), (2, 3), (2, 1)]
    a1=sorted(a,key=lambda x:x[0])
    print(a1)
    
    a2=sorted(a,key=lambda x:x[1])
    print(a2)
a()

https://colab.research.google.com/drive/10whQY5LVOcMW1FIE6I2FqJyvTBzUTKNp

 

day_14.ipynb

Colaboratory notebook

colab.research.google.com

day14

 

def my_gen(low, high):
    while low<=high:
        yield low
        low += 2

lst = []
for i in my_gen(1, 10):
    lst.append(i)
print(lst)

결과

[1,3,5,7,9]

 

Matplot

1. 서울과 부산의 날씨를 그래프로 나타내세요.
x = [ 'Mon', 'Tue', 'Wed', 'Thur', 'Fri',  'Sat', 'Sun' ]
y1 = [15.6, 14.2, 16.3, 18.2, 17.1, 20.2, 22.4] #서울 날씨
y2 = [20.1, 23.1, 23.8, 25.9, 23.4, 25.1, 26.3] #부산 날씨
import matplotlib.pyplot as plt
def a():
    x = [ 'Mon', 'Tue', 'Wed', 'Thur', 'Fri',  'Sat', 'Sun' ]
    y1 = [15.6, 14.2, 16.3, 18.2, 17.1, 20.2, 22.4] #서울 날씨
    y2 = [20.1, 23.1, 23.8, 25.9, 23.4, 25.1, 26.3] #부산 날씨
    plt.plot(x,y1,'o',label='Seoul')
    plt.plot(x,y2,'v',label='Busan')
    plt.legend()
    plt.title('Temperature of two cities')
    plt.xlabel('Day')
    plt.ylabel('Temperature')
    plt.show()


a()

교수님 풀이

def q13_3():
    sales = []
    with open('sales.txt') as f: 
        '''
        for x in f: 
            sales.append(int(x))
        '''
        #혹은 
        sales = [int(x) for x in f.readlines()]
    print(f'총매출: {sum(sales):,}원')
    print(f'평균 일매출: {sum(sales)/len(sales):,}원')
    
def q10_3(): 
    persons = [('GilDong', 'Hong', 27), ('SunSin', 'Lee', 46), ('YuSin', 'Gim', 34)]  
    print(sorted(persons, key = lambda x: x[1]))
    print(sorted(persons, key = lambda x: x[2], reverse = True))
    
def q10_14_15(): 
    fruits = {'Apple': '사과', 'Strawberry': '딸기', 'Peach': '복숭아', 'Grape': '포도'}
    
    #lst = [f'{k}={fruits[k]}' for k in fruits]
    
    #혹은 
    lst = [f'{k}={v}' for (k, v) in fruits.items()]
    print(lst)
    #dct = dict((x.split('=')[0], x.split('=')[1]) for x in lst)
    #혹은
    dct = dict(x.split('=') for x in lst)
    print(dct)
    
def q10_24(): 
    a = [(2, 5), (1, 4), (4, 7), (2, 3), (2, 1)]
    a1 = sorted(a, key = lambda x: x[0])
    a2 = sorted(a, key = lambda x: x[1])
    print(a1)
    print(a2)
q10_24()

 

'파이썬 -23여름학기(ㄱㅎㅈ)' 카테고리의 다른 글

7/12-파일처리  (0) 2023.07.12
이터레이터 추가 학습 in Youtube  (0) 2023.07.11
7/11 12장  (0) 2023.07.11
test5-GUI & datetime test  (0) 2023.07.11
HW10  (0) 2023.07.11