Python Dictionaries
Dictionary
دیکشنری مجموعه ی بی نظمی است با قابلیت تغیر, و به صورت indexed (فهرستی), در پایتون دیکشنری ها را با { } می نویسیم دیکشنری ها به صورت key : Value ذخیره میشوند.
مثال:
:Create and print a dictionary
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Accessing Items
ما می توانیم با اشاره به نام های کلید یک دیکشنری به آیتم های آن که درون {} است دسترسی داشته باشیم.
مثال:
:Get the value of the “model” key
x = thisdict["model"]
در اینجا متد ( )get را داریم که به ما همان نتیجه را می دهد.
مثال:
:Get the value of the “model” key
x = thisdict.get("model")
Change Values
ما می توانیم با اشاره به نام کلید یک آیتم خاص مفدار آن را تغیر دهیم.
مثال:
:Change the “year” to 2018
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
Loop Throygh a Dictionary
ما می توانیم با استفاده از حلقه for درون یک دیکشنری پیمایش کنیم.
وقتی درون یک دیکشنری حلقه ایجاد می کنیم کلید دیکشنری را بر می گرداند البته متدهایی برای برگرداندن مقدار نیز وجود دارد.
مثال:
:Print all key names in the dictionary, one by one
for x in thisdict:
print(x)
مثال:
:Print all values in the dictionary, one by one
for x in thisdict:
print(thisdict[x])
مثال:
:You can also use the values() function to return values of a dictionary
for x in thisdict.values():
print(x)
مثال:
:Loop through both keys and values, by using the items () function
for x, y in thisdict.items():
print(x, y)
check if key Exists
برای تعیین وجود یک کلمه کلیدی در یک دیکشنری از کلمه کلیدی استفاده می کنیم.
مثال:
:Check if “model” is present in the dictionary
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
Dictiondry Length
برای تعیین تعداد آیتم های یک دیکشنری (شامل مقدار و کلید) از متد ( )len استفاده می کنیم.
مثال:
:Print the number of items in the dictionary
print(len(thisdict))
Adding Items
اضافه کردن یک مورد به دیکشنری با استفاده از اندیس و اختصاص مقدار به آن انجام می پذیرد.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
Removing Items
برای حذف یک آیتم از دیکشنری روش های مختلفی وجود دارد:
مثال:
:the pop() method removes the item with the specified key name
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
متد ()popitem آخرین آیتم وارد شده را حذف می کند.(در ورژن های قبل 3.7 پایتون به صورت تصادفی یک آتم را حذف می شد.)
مثال:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
مثال:
:The del( ) keyword removes the item with the specified key name
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
⊗مثال:
:The del( ) keyword can also delete the dictionary completely
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer exists.
مثال:
:The clear( ) keyword empties the dictionary
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
The dict( ) Conctructor
این امکان وجود دارد تا با استفاده از تابع سازنده dict() یک دیکشنری درست کنیم.
مثال:
thisdict = dict(brand="Ford", model="Mustang", year=1964)
# note that keywords are not string literals
# note the use of equals rather than colon for the assignment
print(thisdict)
Dictionary Methods
در پایتون یک سری متدهای از پیش آماده شده وجود دارد که برای دیکشنری ها می توان از آن استفاده کرد.
