Hướng dẫn ordereddict.fromkeys python
Hàm Dictionary fromkeys() trong Python được sử dụng để tạo một Dictionary mới từ dãy seq và value. Trong đó dãy seq tạo nên các key và tất cả các key chia sẻ các giá trị từ value. Trong trường hợp value không được cung cấp thì value của các key là None. Show
Nội dung chính
Cú phápCú pháp của fromkeys() trong Python: dict.fromkeys(seq[, value])) Tham số:
Ví dụ 1: tạo một Dictionary từ một tập các keysVí dụ sau minh họa cách sử dụng của fromkeys() trong Python. # keys nguyen am keys = {'a', 'e', 'i', 'o', 'u' } dict1 = dict.fromkeys(keys) print(dict1) Chạy chương trình Python trên sẽ cho kết quả: {'o': None, 'u': None, 'i': None, 'e': None, 'a': None} Ví dụ 2: tạo một Dictionary từ một tập các keys với valueVí dụ sau minh họa cách sử dụng của fromkeys() trong Python. # keys nguyen am keys = {'a', 'e', 'i', 'o', 'u' } value = 'nguyen am' dict1 = dict.fromkeys(keys, value) print(dict1) Chạy chương trình Python trên sẽ cho kết quả: {'u': 'nguyen am', 'e': 'nguyen am', 'a': 'nguyen am', 'o': 'nguyen am', 'i': 'nguyen am'} Ví dụ 3: tạo một Dictionary từ một tập các keys với list đối tượng có thể thay đổiVí dụ sau minh họa cách sử dụng của fromkeys() trong Python. # keys nguyen am keys = {'a', 'e', 'i', 'o', 'u' } value = [1] dict1 = dict.fromkeys(keys, value) print(dict1) value.append(2) print(dict1) Chạy chương trình Python trên sẽ cho kết quả: {'i': [1], 'o': [1], 'e': [1], 'u': [1], 'a': [1]} {'i': [1, 2], 'o': [1, 2], 'e': [1, 2], 'u': [1, 2], 'a': [1, 2]} The Example
fromkeys() SyntaxThe syntax of the
Here, alphabets and numbers are the key and value of the dictionary. fromkeys() ParametersThe
Note: The same value is assigned to all the keys of the dictionary. fromkeys() Return ValueThe
Note: If the value of the dictionary is not provided, Example 1: Python Dictionary fromkeys() with Key and Value
Output {'a': 'vowel', 'u': 'vowel', 'e': 'vowel', 'i': 'vowel', 'o': 'vowel'} In the above example, we have used the Here, the Example 2: fromkeys() without Value
Output {1: None, 2: None, 4: None} In the above example, we have created a dictionary named We have not provided any values, so all the keys are assigned Example 3: fromkeys() To Create A Dictionary From Mutable Object
Output {'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1]} {'a': [1, 2], 'u': [1, 2], 'o': [1, 2], 'e': [1, 2], 'i': [1, 2]} In the above example, we have used a list as the value for the dictionary. The iterables like list, dictionary, etc are the mutable objects, meaning they can be modified. Here, when we update the list value using append, the keys are assigned with the new updated values. This is because each element is pointing to the same address in the memory. To solve this problem, we can use dictionary comprehension. Dictionary comprehension for mutable objectsWe can use dictionary comprehension and prevent updating the dictionary when the mutable object (list, dictionary, etc) is updated. For example,
Output {'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1]} {'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1]} In the above example, we have used dictionary comprehension to create a dictionary named vowels. Here, the value is not assigned to the keys of the dictionary. But, for each key in keys, a new list of value is created. The newly created list is assigned each key in the dictionary. Recommended Reading: Python Dictionary Comprehension. |