티스토리 뷰
new_dict = { new_key : new_value for item in list }
new_dict = { new_key : new_value for (key, value) in dict.items() }
new_dict = { new_key : new_value for (key, value) in dict.items() if test }
>> item은 속성값이 아니므로 item() 괄호를 반드시 붙여야 함
ex) names 리스트와 random 점수를 이용해 student_scores 딕셔너리를 이름, 점수로 생성
import random
names = ['Alex', 'Kio', 'Freebee', 'Susan', 'Dave', 'Caroline', 'Beth', 'Freddie']
students_scores = {student:random.randint(0,100) for student in names}
print(students_scores)
{'Alex': 38, 'Kio': 71, 'Freebee': 59, 'Susan': 34, 'Dave': 66, 'Caroline': 2, 'Beth': 40, 'Freddie': 66}
ex) dictionary comprehension 으로 생성한 students_scores 딕셔너리를 이용해서
일정 점수 이상의 student를 dictionary comprehension을 이용해 다시 생성
import random
names = ['Alex', 'Kio', 'Freebee', 'Susan', 'Dave', 'Caroline', 'Beth', 'Freddie']
students_scores = {student:random.randint(0,100) for student in names}
print(students_scores)
for key in students_scores:
print(students_scores[key])
#passed_students = {
# student:students_scores[student] for student in students_scores
# if students_scores[student] >= 50
# }
passed_students = {
student:score for (student, score) in students_scores.items()
if score >= 50
}
print(passed_students)
{'Alex': 37, 'Kio': 85, 'Freebee': 57, 'Susan': 1, 'Dave': 96, 'Caroline': 60, 'Beth': 14, 'Freddie': 70}
37
85
57
1
96
60
14
70
{'Kio': 85, 'Freebee': 57, 'Dave': 96, 'Caroline': 60, 'Freddie': 70}
ex) 단어별 글자수를 딕셔너리 comprehension으로 나타내기
sentence = "What is the Airspeed Velocity of an Unladen Swallow?"
# Don't change code above ?
# Write your code below:
result = {word:len(word) for word in sentence.split()}
print(result)
{'What': 4, 'is': 2, 'the': 3, 'Airspeed': 8, 'Velocity': 8, 'of': 2, 'an': 2, 'Unladen': 7, 'Swallow?': 8}
ex) 섭씨 온도 데이터를 화씨로 변환
weather_c = {
"Monday": 12,
"Tuesday": 14,
"Wednesday": 15,
"Thursday": 14,
"Friday": 21,
"Saturday": 22,
"Sunday": 24,
}
# ? Don't change code above ?
# Write your code ? below:
weather_f = {day:(temp*9/5+32) for (day,temp) in weather_c.items()}
print(weather_f)
{'Monday': 53.6, 'Tuesday': 57.2, 'Wednesday': 59.0, 'Thursday': 57.2, 'Friday': 69.8, 'Saturday': 71.6, 'Sunday': 75.2}
** pandas DataFrame에서의 Loop
#Looping through dictionaries:
student_dict = {
"student": ["Kio", "James", "Lily"],
"score": [95, 55, 99]
}
for (key, value) in student_dict.items():
print(key)
student
score
print(value)
['Kio', 'James', 'Lily']
[95, 55, 99]
student_dict = {
"student": ["Kio", "James", "Lily"],
"score": [95, 55, 99]
}
for (key, value) in student_dict.items():
print(value)
print("\n")
import pandas
student_data_frame = pandas.DataFrame(student_dict)
print(student_data_frame)
['Kio', 'James', 'Lily']
[95, 55, 99]
student score
0 Kio 95
1 James 55
2 Lily 99
#Loop through a DataFrame
for (key, value) in student_data_frame.items():
print(key)
print(value)
student
0 Kio
1 James
2 Lily
Name: student, dtype: object
score
0 95
1 55
2 99
Name: score, dtype: int64
>>>>> output이 그닥 유용하게 사용될 것 같지 않음
#pandas의 iterrows()
#Loop through rows of a DataFrame
for (index, row) in student_data_frame.iterrows():
print(index)
0
1
2
for (index, row) in student_data_frame.iterrows():
print(row)
student Kio
score 95
Name: 0, dtype: object
student James
score 55
Name: 1, dtype: object
student Lily
score 99
Name: 2, dtype: object
>> 특정 열 아래의 값을 구할 수 있다!!
for (index, row) in student_data_frame.iterrows():
print(row.student)
for (index, row) in student_data_frame.iterrows():
if row.student == "Kio":
print(row.score)
Kio
James
Lily
95
'Test > Python(20220101~)' 카테고리의 다른 글
Days27. tkinter, *args, **kwargs, GUI (0) | 2022.03.27 |
---|---|
Days26. 입력하는 영단어를 NATO Phonetic Alphabet 으로 변환 (0) | 2022.03.26 |
Days26. List Comprehension (2) | 2022.03.14 |
cf. Get mouse click coordinates in Python turtle (0) | 2022.03.12 |
Days25. csv 데이터, pandas 라이브러리 (0) | 2022.03.08 |
- Total
- Today
- Yesterday
- Join
- set()
- 읽어오기
- LIST
- 차집합
- insert
- exadata
- 부동없이
- 스토리지
- powercli
- 정렬
- artandculture
- 중복제거
- 3par
- virt-sysprep
- vmware
- sysprep
- oracle
- storage
- EXA
- fromkeys
- dp-1
- 변수화
- dp-2
- vmware.powercli
- cloud-init
- 대소문자
- powershell
- 배열
- dezoomify
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |