dictionary

Published by onesixx on

만들기

dict_a = dict(apple=300, pear=250, peach=400)
dict_a = {'apple':300, 'pear':250, 'peach':400}
dict_family = {}
dict_family.setdefault('emily', 1)
dict_family.setdefault('sixx', 6)
dict_family.setdefault('sdragon', 7)

col_unique = [col for col in df.columns if df[col].nunique()<20]
{col: sorted(list(df[col].unique()))  for col in col_unique}

keys()

딕셔너리의 Key만 뽑아 리스트 또는 튜플 형태로 변환

list(dict_a.keys())
# ['apple', 'pear', 'peach']

values()

딕셔너리의 Value만 뽑아 리스트 또는 튜플 형태로 변환

list(dict_a.values())
# [300, 250, 400]

items()

 for k, v in dict_family.items():
    print(k,v)
 # sixx 6
 # emily 1
 # sdragon 7

list(dict_a.items())
# [('apple', 300), ('pear', 250), ('peach', 400)]

[ [k,v] for k,v in dict_a.items()]
# [['apple', 300], ['pear', 250], ['peach', 400]]

삭제

특정 Key 삭제
del dict_a['apple']
#{'pear': 250, 'peach': 400}

모두 삭제 
dict_a.clear()
# {}

정렬

key기준

value기준

dict_a = {'apple':300, 'pear':250, 'peach':400}


##### key기준 -----------------------------------
sorted(dict_a)
# ['apple', 'peach', 'pear']

sorted(dict_a.items())
# [('apple', 300), ('peach', 400), ('pear', 250)]
dict(sorted(dict_a.items()))
# {'apple': 300, 'peach': 400, 'pear': 250}


##### value기준 -----------------------------------
dict(sorted(dict_a.items(), key=lambda x:x[1]))
# {'pear': 250, 'apple': 300, 'peach': 400}

zip(변수, 변수)

집합 변수를 만들어주는 함수.
(묶음 형태의 딕셔너리, 튜플, 리스트으로 만들수도 있다.)

k = ['apple', 'pear', 'peach']
v = [300, 250, 400]

dict(zip(k,v))
# {'apple': 300, 'pear': 250, 'peach': 400}

list(zip(k,v))
# [('apple', 300), ('pear', 250), ('peach', 400)]
tuple(zip(k,v))
#(('apple', 300), ('pear', 250), ('peach', 400))
https://bluese05.tistory.com/67

Dictionary Slice

import json

dict_a = {
    "C": 1.1,
    "O": True,
    "M": "HelloWorld",
    "P": {
        "X": 1,
        "Y": 2,
        "Z": 3
    },
    "U": 25,
    "T": None,
    "E": ["Python", "Is", "Beautiful"],
    "R": [1, 2, 100, 200]
}
targetKey = ["C", "P", "U"]

targe이 정해져 있는 경우

### using dictionary 
result = {k:dict_a[k] for k in targetKey}

result = {k:dict_a[k] for k in dict_a.keys() if k in targetKey}

result = {k:v for k,v in dict_a.items() if k in targetKey}

# {'C': 1.1, 'P': {'X': 1, 'Y': 2, 'Z': 3}, 'U': 25}
print(json.dumps(result, indent = 4))

### using List
result={}
for k,v in dict_a.items(): 
    if k in targetKey:
        result[k] = v
        

itertools for slice

### itertools
import itertools
dict( itertools.islice( dict_a.items(), 2))
# {'C': 1.1, 'O': True}
from itertools import islice

dict_family = {'sixx': 6, 'emily': 7, 'sdragon': 2, 'john': 5, 'alice': 4}

first_three = dict(islice(dict_family.items(), 3))

# {'sixx': 6, 'emily': 7, 'sdragon': 2}

dict(islice(dict_family.items(), 1, 3))
# {'emily': 7, 'sdragon': 2}
from itertools import islice

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Extract elements from index 2 to 6 (exclusive)
# islice(iterable, start, stop, step)
result = islice(my_list, 1, 6, 2) 

for item in result:
    print(item)
    
 # 2 4 6

Categories: Python Basic

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x