دیکشنری در زبان برنامه نویسی پایتون یک نوع دادهای محبوب و نسبتاً پر کاربرد است. این نوع دادهای در اصل یک نوع لیست انجمنی (Associative) یا لیست کلید-مقدار (key-value) هست. معمولاً از دیکشنری پایتون در جاهایی استفاده میکنیم که نیاز داریم مقادیری را با استفاده از یک کلید مرتبط با هر کدام از آنها شناسایی کنیم. این مقادیر هر چیزی میتوانند باشند؛ برای مثال، رشته متنی، عدد، شئ و … .
دیکشنری (Dictionary) در زبان فارسی معادل فرهنگ لغت یا واژهنامه است. در یک کتاب فرهنگ لغت، یک کلمه با معادلش مرتبط میشود. این معادل میتواند یکی بوده یا چندین مورد باشد، اما کلمه اصلی یکی است! در اصل یک یا چند معنی معادل را به یک کلمه مرتبط کردهایم.
در دیکشنری پایتون هم دقیقاً مشابه چنین کاری را انجام میدهیم؛ یک کلمه (که آن را به عنوان کلید میشناسیم) با یک مقدار (string، عدد، رفرنس شئ و …) مرتبط خواهد شد.
.
clc;clear;
%Dictionary
studentID=py.dict(pyargs('Ali',250,'Mohammad',364,'Soheyl',430))
% Dictionary update
studentID.update(py.dict(pyargs('Alireza', 500))); % Update the dictionary with a new student
disp(studentID)
% Get the length of the dictionary
length_student = py.len(studentID)
% Get the minimum value of the studentID
min_value = py.min(studentID)
% Get the maximum value of the studentID
max_value = py.max(studentID)
% Get the keys from the 'studentID' dictionary
keys = studentID.keys() % This will return a list of all keys in the 'studentID' dictionary
% Get the values from the 'studentID' dictionary
values = studentID.values() % This will return a list of all values in the 'studentID' dictionary
% Get the items from the 'studentID' dictionary
items = studentID.items() % This will return a list of tuples, each containing a key-value pair from the 'studentID' dictionary
دیدگاهها
هیچ نظری هنوز ثبت نشده است.