file = open('data.txt') # 기본값 : 읽기(read) 모드
content = file.read()
file.close() # 무조건 close() 해줘야 됨
with open('data.txt') as file:
content = file.read() # 들여쓰기가 끝나면 file이 자동으로 닫힌다.
# file.close() - 필요 없음
contents = []
with open('data.txt') as file:
for line in file:
contents.append(line)
# 쓰기(write) 모드
with open('data.txt', 'w') as file:
file.write('Hello')
apple = ('사과', 'apple', 'pomme')
numbers = [-1, 3, -4, 5, 6, 100]
sort_by_abs = sorted(numbers, key=abs)
# key가 abs이므로 abs()에 넣었을 때 나오는 결과값을 기준으로 정렬
# [-1, 3, -4, 5, 6, 100]
def reverse(word):
return str(reversed(word))
fruits = ['cherry', 'apple', 'banana']
sort_by_last = sorted(fruits, key=reverse)
ex) reverse('apple') = 'elppa'
# 문자열을 넣었을 때 거꾸로 나온 값을 기준으로 정렬한다.
# ['banana', 'apple', 'cherry']
import collections import Counter
cnt = Counter()
for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']
cnt[word] += 1
cnt # Counter({'blue': 3, 'red': 2, 'green': 1})
Counter('hello world')
# Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
출처: 엘리스 AI트랙 3기 11주차 수업