python 기본 읽기 파일
python 기본 읽기 파일 '''
Lesson on file reading using Airline Safety Data
https://github.com/fivethirtyeight/data/tree/master/airline-safety
'''
# read the whole file at once, return a single string (including newlines)
# 'rU' mode (read universal) converts different line endings into '
'
f = open('airlines.csv', mode='rU')
file_string = f.read()
f.close()
# use a context manager to automatically close your file
with open('airlines.csv', mode='rU') as f:
file_string = f.read()
# read the file into a list (each list element is one row)
with open('airlines.csv', mode='rU') as f:
file_list = []
for row in f:
file_list.append(row)
# do the same thing using a list comprehension
with open('airlines.csv', mode='rU') as f:
file_list = [row for row in f]
# side note: splitting strings
'hello DAT students'.split()
'hello DAT students'.split('e')
# split each string (at the commas) into a list
with open('airlines.csv', mode='rU') as f:
file_nested_list = [row.split(',') for row in f]
# do the same thing using the csv module
import csv
with open('airlines.csv', mode='rU') as f:
file_nested_list = [row for row in csv.reader(f)]
# separate the header and data
header = file_nested_list[0]
data = file_nested_list[1:]
'''
EXERCISES:
1. Create a list containing the average number of incidents per year for each airline.
Example for Aer Lingus: (2 + 0)/30 = 0.07
Expected output: [0.07, 2.73, 0.23, ...]
2. Create a list of airline names (without the star).
Expected output: ['Aer Lingus', 'Aeroflot', 'Aerolineas Argentinas', ...]
3. Create a list (of the same length) that contains 1 if there's a star and 0 if not.
Expected output: [0, 1, 0, ...]
4. BONUS: Create a dictionary in which the key is the airline name (without the star)
and the value is the average number of incidents.
Expected output: {'Aer Lingus': 0.07, 'Aeroflot': 2.73, ...}
'''
# Part 1
incidents = [round((int(row[2]) + int(row[5])) / float(30), 2) for row in data]
# Parts 2 and 3
airlines = []
starred = []
for row in data:
if row[0][-1] == '*':
starred.append(1)
airlines.append(row[0][:-1])
else:
starred.append(0)
airlines.append(row[0])
# Part 4
airline_incidents = dict(zip(airlines, incidents))
'''
A few extra things that will help you with the homework
'''
# 'set' data structure is useful for gathering unique elements
my_list = [1, 2, 1]
set(my_list) # returns a set of 1, 2
len(set(my_list)) # count of unique elements
# 'in' statement is useful for lists
1 in my_list # True
3 in my_list # False
# 'in' is useful for strings (checks for substrings)
my_string = 'hello there'
'the' in my_string # True
'then' in my_string # False
# 'in' is useful for dictionaries (checks keys but not values)
my_dict = {'name':'Kevin', 'title':'instructor'}
'name' in my_dict # True
'Kevin' in my_dict # False
# 'count' method for strings counts how many times a character appears
my_string.count('e') # 3
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSON
JSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다.
그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다.
저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
'''
Lesson on file reading using Airline Safety Data
https://github.com/fivethirtyeight/data/tree/master/airline-safety
'''
# read the whole file at once, return a single string (including newlines)
# 'rU' mode (read universal) converts different line endings into '
'
f = open('airlines.csv', mode='rU')
file_string = f.read()
f.close()
# use a context manager to automatically close your file
with open('airlines.csv', mode='rU') as f:
file_string = f.read()
# read the file into a list (each list element is one row)
with open('airlines.csv', mode='rU') as f:
file_list = []
for row in f:
file_list.append(row)
# do the same thing using a list comprehension
with open('airlines.csv', mode='rU') as f:
file_list = [row for row in f]
# side note: splitting strings
'hello DAT students'.split()
'hello DAT students'.split('e')
# split each string (at the commas) into a list
with open('airlines.csv', mode='rU') as f:
file_nested_list = [row.split(',') for row in f]
# do the same thing using the csv module
import csv
with open('airlines.csv', mode='rU') as f:
file_nested_list = [row for row in csv.reader(f)]
# separate the header and data
header = file_nested_list[0]
data = file_nested_list[1:]
'''
EXERCISES:
1. Create a list containing the average number of incidents per year for each airline.
Example for Aer Lingus: (2 + 0)/30 = 0.07
Expected output: [0.07, 2.73, 0.23, ...]
2. Create a list of airline names (without the star).
Expected output: ['Aer Lingus', 'Aeroflot', 'Aerolineas Argentinas', ...]
3. Create a list (of the same length) that contains 1 if there's a star and 0 if not.
Expected output: [0, 1, 0, ...]
4. BONUS: Create a dictionary in which the key is the airline name (without the star)
and the value is the average number of incidents.
Expected output: {'Aer Lingus': 0.07, 'Aeroflot': 2.73, ...}
'''
# Part 1
incidents = [round((int(row[2]) + int(row[5])) / float(30), 2) for row in data]
# Parts 2 and 3
airlines = []
starred = []
for row in data:
if row[0][-1] == '*':
starred.append(1)
airlines.append(row[0][:-1])
else:
starred.append(0)
airlines.append(row[0])
# Part 4
airline_incidents = dict(zip(airlines, incidents))
'''
A few extra things that will help you with the homework
'''
# 'set' data structure is useful for gathering unique elements
my_list = [1, 2, 1]
set(my_list) # returns a set of 1, 2
len(set(my_list)) # count of unique elements
# 'in' statement is useful for lists
1 in my_list # True
3 in my_list # False
# 'in' is useful for strings (checks for substrings)
my_string = 'hello there'
'the' in my_string # True
'then' in my_string # False
# 'in' is useful for dictionaries (checks keys but not values)
my_dict = {'name':'Kevin', 'title':'instructor'}
'name' in my_dict # True
'Kevin' in my_dict # False
# 'count' method for strings counts how many times a character appears
my_string.count('e') # 3
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.