πŸ“—
JunegLee's TIL
  • TIL
  • python
    • class
    • String Basic
    • regularExpression
    • String function
    • Generator
    • String format
    • getset
    • module
    • while
    • numpy
    • print()
    • matplotlib
    • for
    • Boolean
    • tuple
    • package
    • input(variable)
    • list
    • if
    • file
    • type()
    • pandas
    • function
    • dictionary
    • ꡬ문 였λ₯˜μ™€ μ˜ˆμ™Έ
    • builtinFunction
    • Constructor
  • algorithm
    • sort
      • mergeSort
      • insertionSort
      • bubbleSort
      • heapSort
      • quickSort
      • selectionSort
    • recursion
    • Greedy
    • DepthFirstSearch
    • basic
      • DataStructure
    • hash
    • BreadthFirstSearch
  • tensorflow
    • keras
      • layers
        • Flatten
        • Flatten
        • Dense
        • Dense
        • Conv2D
        • Conv2D
    • tensorflow1x
    • tensorflow2x
  • DB
    • setting
    • join
    • subQuery
    • overview
  • deep-learning
    • neuralNetwork
    • perceptron
    • neuralNetworkLearning
    • convolution neural network
    • Gradient Descent
    • Linear Regression
    • backPropagation
    • logistic regression
    • overview
  • textPreprocessing
    • overview
  • java
    • basics
      • generic
      • Variable
      • String
    • theory
      • Object Oriented Programing
  • NLP
    • Embedding
    • Natural Language Processing
Powered by GitBook
On this page
  • dictionary
  • λ¦¬μŠ€νŠΈμ™€ λ”•μ…”λ„ˆλ¦¬ 비ꡐ
  • λ¦¬μŠ€νŠΈμ™€ λ”•μ…”λ„ˆλ¦¬λ₯Ό κ°’μœΌλ‘œ λ„£κΈ°
  • λ”•μ…”λ„ˆλ¦¬μ˜ μš”μ†Œμ— μ ‘κ·Όν•˜κΈ°
  • κ°’ λ³€κ²½
  • λ”•μ…”λ„ˆλ¦¬ μš”μ†Œμ— μΆ”κ°€ : λ”•μ…”λ„ˆλ¦¬[μƒˆλ‘œμš΄ ν‚€] = μƒˆλ‘œμš΄ κ°’
  • λ”•μ…”λ„ˆλ¦¬ μš”μ†Œμ— 제거 : del
  • λ”•μ…”λ„ˆλ¦¬ 내뢀에 ν‚€κ°€ μžˆλŠ”μ§€ 확인
  • λ”•μ…”λ„ˆλ¦¬ λ§Œλ“€ λ•Œ μ£Όμ˜ν•  사항
  • λ”•μ…”λ„ˆλ¦¬ κ΄€λ ¨ ν•¨μˆ˜λ“€

Was this helpful?

  1. python

dictionary

dictionary

λ¦¬μŠ€νŠΈμ™€ λ”•μ…”λ„ˆλ¦¬ 비ꡐ

리슀트

λ”•μ…”λ„ˆλ¦¬

μ •μ˜

인덱슀λ₯Ό 기반으둜 값을 μ €μž₯ν•˜λŠ” 것

ν‚€λ₯Ό 기반으둜 값을 μ €μž₯ν•˜λŠ” 것

μ„ μ–Έ ν˜•μ‹

list_a = []

dict_a = {}

μ‚¬μš© 예

list_a[1]

dict_a["name"]

λ³€μˆ˜ = {
 ν‚€ : κ°’,
 ν‚€ : κ°’,
 ...
 ν‚€ : κ°’
}
dict_a = {
    "name1" : "user1",
    "name2" : "user2"
}
print(dict_a["name1"]) # user1
print(dict_a["name2"]) # user2

λ¦¬μŠ€νŠΈμ™€ λ”•μ…”λ„ˆλ¦¬λ₯Ό κ°’μœΌλ‘œ λ„£κΈ°

dict_b = {
    "dirctor" : ["μ•ˆμ†Œλ‹ˆ","μ‘°"],
    "cast" : ["아이언맨","νƒ€λ…ΈμŠ€","헐크","ν† λ₯΄"]
}
print(dict_b) # {'dirctor': ['μ•ˆμ†Œλ‹ˆ', 'μ‘°'], 'cast': ['아이언맨', 'νƒ€λ…ΈμŠ€', '헐크', 'ν† λ₯΄']}
print(dict_b["dirctor"]) # ['μ•ˆμ†Œλ‹ˆ', 'μ‘°']

λ”•μ…”λ„ˆλ¦¬μ˜ μš”μ†Œμ— μ ‘κ·Όν•˜κΈ°

dictionary = {
    "name": "7D 건쑰 망고",
    "type": "λ‹Ήμ ˆμž„",
    "ingredient": ["망고", "섀탕", "λ©”νƒ€μ€‘μ•„ν™©μ‚°λ‚˜νŠΈλ₯¨", "μΉ˜μžν™©μƒ‰μ†Œ"],
    "origin": "필리핀"
}

print("name:", dictionary["name"]) # name: 7D 건쑰 망고
print("type:", dictionary["type"]) # type: λ‹Ήμ ˆμž„
print("ingredient:", dictionary["ingredient"]) # ingredient: ['망고', '섀탕', 'λ©”νƒ€μ€‘μ•„ν™©μ‚°λ‚˜νŠΈλ₯¨', 'μΉ˜μžν™©μƒ‰μ†Œ']
print("origin:", dictionary["origin"]) # origin: 필리핀
print("--------------------------------------------")

κ°’ λ³€κ²½

dictionary["name"] = "8D 건쑰 망고"
print("name:", dictionary["name"]) #  name: 8D 건쑰 망고

λ”•μ…”λ„ˆλ¦¬ μš”μ†Œμ— μΆ”κ°€ : λ”•μ…”λ„ˆλ¦¬[μƒˆλ‘œμš΄ ν‚€] = μƒˆλ‘œμš΄ κ°’

dictionary = {}
print("μš”μ†Œ μΆ”κ°€ 이전:", dictionary)
dictionary["name"] = "μƒˆλ‘œμš΄ 이름"
dictionary["head"] = "μƒˆλ‘œμš΄ μ •μ‹ "
dictionary["body"] = "μƒˆλ‘œμš΄ λͺΈ"
print("μš”μ†Œ μΆ”κ°€ 이후:", dictionary)

λ”•μ…”λ„ˆλ¦¬ μš”μ†Œμ— 제거 : del

dictionary2 = {
    "name": "7D 건쑰 망고",
    "type": "λ‹Ήμ ˆμž„"
    }

print("μš”μ†Œ 제거 이전:", dictionary2) # μš”μ†Œ 제거 이전: {'name': '7D 건쑰 망고', 'type': 'λ‹Ήμ ˆμž„'}
del dictionary2["name"]
del dictionary2["type"]
print("μš”μ†Œ 제거 이후:", dictionary2) # μš”μ†Œ 제거 이후: {}

λ”•μ…”λ„ˆλ¦¬ 내뢀에 ν‚€κ°€ μžˆλŠ”μ§€ 확인

: in ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 내뢀에 ν‚€κ°€ μžˆλŠ”μ§€ μ—†λŠ”μ§€ 확인

dictionary = {
    "name": "7D 건쑰 망고",
    "type": "λ‹Ήμ ˆμž„",
    "ingredient": ["망고", "섀탕", "λ©”νƒ€μ€‘μ•„ν™©μ‚°λ‚˜νŠΈλ₯¨", "μΉ˜μžν™©μƒ‰μ†Œ"],
    "origin": "필리핀"
}
key = input("> μ ‘κ·Όν•˜κ³ μž ν•˜λŠ” ν‚€: ")
if key in dictionary:
    print(dictionary[key])
else:
    print("μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” 킀에 μ ‘κ·Όν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.")

λ”•μ…”λ„ˆλ¦¬ λ§Œλ“€ λ•Œ μ£Όμ˜ν•  사항

  1. KeyλŠ” κ³ μœ ν•œ κ°’μ΄λ―€λ‘œ μ€‘λ³΅λ˜λŠ” Key 값을 μ„€μ •ν•΄ λ†“μœΌλ©΄ ν•˜λ‚˜λ₯Ό μ œμ™Έν•œ λ‚˜λ¨Έμ§€ 것듀이 λͺ¨λ‘ λ¬΄μ‹œ

    a = {1:'a', 1:'b'}
    print(a) # {1: 'b'}
  2. Key에 λ¦¬μŠ€νŠΈλŠ” μ“Έ 수 μ—†λ‹€

    a = {[1,2] : 'hi'}
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'list'

λ”•μ…”λ„ˆλ¦¬ κ΄€λ ¨ ν•¨μˆ˜λ“€

  • Key 리슀트 λ§Œλ“€κΈ° : keys

    a = {'name': 'pey', 'phone': '0119993323', 'birth': '1118'}
    print(a.keys()) # dict_keys(['name', 'phone', 'birth'])
    # 리슀트둜 λ°˜ν™˜ν•˜λŠ” 방법 : list 
    list(a.keys()) # ['name', 'phone', 'birth']
  • Value 리슀트 λ§Œλ“€κΈ° : values

    a.values() # dict_values(['pey', '0119993323', '1118'])
  • Key, Value 쌍 μ–»κΈ° : items

    a.items() # dict_items([('name', 'pey'), ('phone', '0119993323'), ('birth', '1118')])
  • Key: Value 쌍 λͺ¨λ‘ μ§€μš°κΈ° : clear

    a.clear()
    print(a) # {}
  • Key둜 Valueμ–»κΈ° : get

    a = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
    print(a.get('name')) # 'pey'
    # μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” 킀에 μ ‘κ·Όν•˜λŠ” μƒν™©μ—μ„œ λ‘λ²ˆμ§Έ λŒ€μ²˜ λ°©λ²•μœΌλ‘œλŠ” λ”•μ…”λ„ˆλ¦¬μ˜ get()
    value = dictionary.get("none")
    print("κ°’:", value)
    if value == None:
      print("μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” 킀에 μ ‘κ·Όν–ˆμ—ˆμŠ΅λ‹ˆλ‹€.")
  • ν•΄λ‹Ή Keyκ°€ λ”•μ…”λ„ˆλ¦¬ μ•ˆμ— μžˆλŠ”μ§€ μ‘°μ‚¬ν•˜κΈ° : in

    a = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
    'name' in a
    # True
PreviousfunctionNextꡬ문 였λ₯˜μ™€ μ˜ˆμ™Έ

Last updated 4 years ago

Was this helpful?