تحويلات بنية البيانات
تعلم كيفية التحويل بين القوائم والقواميس والمجموعات باستخدام طرق صريحة في Python.
سنغطي ما يلي...
قبل الخوض في الطرق المُدمجة المُستخدمة للتحويل بين أنواع البيانات، دعونا نُلخص الفروقات بينها. يُلخص الجدول التالي هذه الفروقات بوضوح.
Feature | Tuple | List | Dictionary | Set |
Bracket type |
|
|
|
|
Creation |
|
|
|
|
Mutability | Immutable | Mutable | Mutable | Mutable (elements are immutable) |
Order | Maintains order | Maintains order | No inherent order | No specific order elements are unique |
Duplicates | Allows duplicates | Allows duplicates | Keys must be unique; values may repeat | No duplicates |
Access | Indexing (my_tuple[0]) | Indexing (my_list[0]) | Key-based (my_dict['key1']) | No indexing; use iteration or in operator |
Operations | Limited concatenation, slicing | Extensive concatenation, slicing | Key-based operations | Set operations |
Use Cases | Fixed collection of items | A dynamic collection of items | Associative arrays, key-value pairs | Unique collection, membership testing |
تحويل صريح
القالب للتحويل صراحة من بنية بيانات إلى أخرى هو كما يلي:
destination_structure_name(source_structure_object)