Hướng dẫn convert text to dictionary python - chuyển văn bản sang từ điển python

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận 

    Sự xen kẽ của các loại dữ liệu đã được thảo luận nhiều lần và là một vấn đề khá phổ biến để giải quyết. Bài viết này thảo luận về một vấn đề khác về sự xen kẽ của từ điển, ở định dạng chuỗi thành một từ điển. Hãy để thảo luận về những cách nhất định trong đó điều này có thể được thực hiện.

    Python3

    Phương thức số 1: Sử dụng json.loads () & nbsp;

    Nhiệm vụ này có thể dễ dàng được thực hiện bằng cách sử dụng chức năng sẵn có của tải thư viện JSON của Python, chuyển đổi chuỗi từ điển hợp lệ thành dạng JSON, từ điển trong Python. & NBSP;

    import json

    test_string ____10

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    1

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    2
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    3
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    4
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    5
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    6
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    7

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    8
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    2
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    3
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    3
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    5
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    6
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    6
     

    Đầu ra: & nbsp;

    Python3

    Phương pháp số 2: Sử dụng ast.literal_eval () & nbsp;

    Nhiệm vụ này có thể dễ dàng được thực hiện bằng cách sử dụng chức năng sẵn có của tải thư viện JSON của Python, chuyển đổi chuỗi từ điển hợp lệ thành dạng JSON, từ điển trong Python. & NBSP;

    import json

    test_string ____10

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    1

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    2
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    3
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    4
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    5
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    6
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    7

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    8
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    2
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    3
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    3
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    5
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    6
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    6
    The above method can also be used to perform a similar conversion. The eval() function parse the argument passed and converts it to a python  expression and runs the python expression.

    Đầu ra: & nbsp;

    Nhiệm vụ này có thể dễ dàng được thực hiện bằng cách sử dụng chức năng sẵn có của tải thư viện JSON của Python, chuyển đổi chuỗi từ điển hợp lệ thành dạng JSON, từ điển trong Python. & NBSP;

    import json

    test_string ____10

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    1

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    2
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    3
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    4
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    5
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    6
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    7

    Output:

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}

    Từ điển là một bộ sưu tập không có thứ tự trong Python lưu trữ các giá trị dữ liệu như bản đồ, tức là, cặp giá trị: cặp giá trị. Để chuyển đổi một chuỗi thành một từ điển, chuỗi được lưu trữ phải theo cách mà khóa: cặp giá trị có thể được tạo từ nó. Bài viết này cho thấy một số cách để chuyển đổi một chuỗi thành một từ điển. Phương pháp 1: Chia một chuỗi để tạo khóa: cặp giá trị của từ điển Trong phương pháp này, chuỗi đã cho sẽ được phân tích và với việc sử dụng phương thức Split (), chuỗi sẽ được chia theo cách mà nó tạo ra khóa: Cặp giá trị cho việc tạo ra một từ điển. Dưới đây là việc thực hiện phương pháp. & NBSP;Method 1: Splitting a string to generate key:value pair of the dictionary In this approach, the given string will be analysed and with the use of split() method, the string will be split in such a way that it generates the key:value pair for the creation of a dictionary. Below is the implementation of the approach. 

    Python3

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    6
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0
    {'Feb': 'February', 'Jan': 'January', 'Mar': 'March'}
    8

    {'Feb': 'February', 'Jan': 'January', 'Mar': 'March'}
    9
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    2json3

    Output:

    {' Feb ': ' February', ' Mar ': ' March', ' Jan ': ' January'}

    Phương pháp 2: Sử dụng 2 chuỗi để tạo khóa: cặp giá trị cho từ điển trong phương pháp này, 2 chuỗi khác nhau sẽ được xem xét và một trong số chúng sẽ được sử dụng để tạo các khóa và một chuỗi khác sẽ được sử dụng để tạo các giá trị cho từ điển. Sau khi thao tác với cả hai chuỗi, các mục từ điển sẽ được tạo bằng cách sử dụng các cặp khóa: giá trị đó. Dưới đây là việc thực hiện phương pháp. & NBSP; In this approach, 2 different strings will be considered and one of them will be used to generate keys and another one will be used to generate values for the dictionary. After manipulating both the strings the dictionary items will be created using those key:value pair. Below is the implementation of the approach. 

    Python3

    json4

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0 json6

    json7

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0 json9

    test_string 0

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0 test_string 2test_string 3test_string 4

    test_string 5

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0 test_string 7test_string 8test_string 4

    {'Feb': 'February', 'Jan': 'January', 'Mar': 'March'}
    9____10
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    02

    Is

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    10
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    11
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    13

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    2json3

    Output:

    {'Jan': 'January ', 'Feb': ' February ', 'March': ' March'}

    Phương pháp 3: Sử dụng phương thức zip () Để kết hợp khóa: cặp giá trị được trích xuất từ ​​2 chuỗi trong phương pháp này, một lần nữa 2 chuỗi sẽ được sử dụng, một chuỗi để tạo các khóa và một chuỗi khác để tạo giá trị cho từ điển. Khi tất cả các khóa và giá trị được lưu trữ, phương thức zip () sẽ được sử dụng để tạo khóa: cặp giá trị và do đó tạo ra từ điển hoàn chỉnh. Dưới đây là việc thực hiện phương pháp. & NBSP; In this approach, again 2 strings will be used, one for generating keys and another for generating values for the dictionary. When all the keys and values are stored, zip() method will be used to create key:value pair and thus generating the complete dictionary. Below is the implementation of the approach. 

    Python3

    json4

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0 json6

    json7

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0 json9

    test_string 0

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0 test_string 2test_string 3test_string 4

    test_string 5

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0 test_string 7test_string 8test_string 4

    {'Feb': 'February', 'Jan': 'January', 'Mar': 'March'}
    9____10
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    02

    Is

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    2json3

    Output:

    {' March': ' March', 'Jan': 'January ', ' Feb': ' February '}

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    10
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    11
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    13
    In this approach, a string which is already in the form of string dictionary i.e., the string has a dictionary expression is converted into a dictionary using ast.literal_eval() method. Below is the implementation of the approach. 

    Python3

    {'Jan': 'January ', 'Feb': ' February ', 'March': ' March'}

    Phương pháp 3: Sử dụng phương thức zip () Để kết hợp khóa: cặp giá trị được trích xuất từ ​​2 chuỗi trong phương pháp này, một lần nữa 2 chuỗi sẽ được sử dụng, một chuỗi để tạo các khóa và một chuỗi khác để tạo giá trị cho từ điển. Khi tất cả các khóa và giá trị được lưu trữ, phương thức zip () sẽ được sử dụng để tạo khóa: cặp giá trị và do đó tạo ra từ điển hoàn chỉnh. Dưới đây là việc thực hiện phương pháp. & NBSP;

    {'Feb': 'February', 'Jan': 'January', 'Mar': 'March'}
    9
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    0 import1
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    3
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    39
    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    40

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    2json3

    Output:

    {'Feb': 'February', 'Jan': 'January', 'Mar': 'March'}

    Phương pháp 4: Nếu chuỗi tự nó ở dạng từ điển chuỗi trong phương pháp này, một chuỗi đã ở dạng từ điển chuỗi, tức là, chuỗi có biểu thức từ điển được chuyển đổi thành một từ điển bằng phương thức AST.Literal_Eval (). Dưới đây là việc thực hiện phương pháp. & NBSP;

    import

    The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
    The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
    8O(n)
    Space Complexity: O(n)


    Một chuỗi có thể là một từ điển trong Python?

    Trong Python, chuỗi được chuyển đổi thành từ điển bằng cách sử dụng hàm json.load ().Đó là chức năng tích hợp.Chúng ta phải nhập thư viện này bằng cách sử dụng từ nhập khẩu từ trước khi hàm này.the string is converted into a dictionary by the use of json. load () function. It is the built-in function. We must import this library by utilizing the “import” word before this function.

    Bạn có thể biến một danh sách thành một python từ điển không?

    Bạn có thể chuyển đổi danh sách Python thành từ điển bằng phương thức Dict.FromKeys (), một cách hiểu từ điển hoặc phương thức zip ().Phương thức Zip () rất hữu ích nếu bạn muốn hợp nhất hai danh sách thành một từ điển. fromkeys() method, a dictionary comprehension, or the zip() method. The zip() method is useful if you want to merge two lists into a dictionary.

    Làm thế nào tôi có thể tạo một từ điển trong Python?

    Tạo từ điển Python Tạo một từ điển cũng đơn giản như việc đặt các mục bên trong niềng răng xoăn {} được phân tách bằng dấu phẩy.Một mục có khóa và giá trị tương ứng được biểu thị dưới dạng cặp (khóa: giá trị).placing items inside curly braces {} separated by commas. An item has a key and a corresponding value that is expressed as a pair (key: value).

    Một từ điển có thể có một chuỗi là giá trị?

    Giá trị từ điển có thể chỉ là bất cứ điều gì (int, danh sách, chức năng, chuỗi, v.v.).Ví dụ, từ điển dưới đây, giới tính có ints là khóa và chuỗi là giá trị. (int, lists, functions, strings, etc). For example, the dictionary below, genderDict has ints as keys and strings as values.