Pythona | Konwertuj obiekt słownika na ciąg znaków

Słownik jest ważnym pojemnikiem i jest używany prawie w każdym kodzie codziennego programowania, a także tworzenia stron internetowych Pyton . Im częściej się je stosuje, tym większa jest potrzeba ich opanowania i dlatego konieczne jest ich poznanie.

  Input:   { 'testname' : 'akshat','test2name' : 'manjeet','test3name' : 'nikhil'}   Output:   {'testname': 'akshat', 'test2name': 'manjeet', 'test3name': 'nikhil'}   Explanation:   Input type is but the output type is 

Zobaczmy różne sposoby zmiany słownika na ciąg znaków.

Obiekt słownikowy w ciągu znaków Rozmowa

Poniżej znajdują się metody, które omówimy w tym artykule:

Konwersja dyktowania na ciąg w Pythonie przy użyciu metody json.dumps().

Tutaj możemy użyć metody dump() z pliku JSON bibliotekę, importując ją, co konwertuje typ danych słownika na ciąg. W poniższym kodzie najpierw wykonujemy test słownikowy1, a następnie go używamy json.dumps metodę i przekaż w niej słownik tes1, a otrzymamy wymagany wynik w pliku strunowy format.

Python3




import> json> # initialising dictionary> test1> => {> 'testname'> :> 'akshat'> ,> > 'test2name'> :> 'manjeet'> ,> > 'test3name'> :> 'nikhil'> }> # print original dictionary> print> (> type> (test1))> print> (> 'initial dictionary = '> , test1)> # convert dictionary into string> result> => json.dumps(test1)> # printing result as string> print> (> ' '> ,> type> (result))> print> (> 'final string = '> , result)>

Wyjście:

initial dictionary = {‘testname’: ‘akshat’, ‘test2name’: ‘manjeet’, ‘test3name’: ‘nikhil’} final string = {testname: akshat, test2name: manjeet, test3name: nikhil} 

Złożoność przestrzeni: NA)
Złożoność czasowa: NA)

Słownik konwersji na ciąg znaków przy użyciu funkcji str().

The str() funkcja konwertuje określoną wartość na ciąg znaków. Funkcja string jest również pomocna przy konwersji typu danych na typ string. W tym celu przekazujemy słownik do tej metody, która przekonwertuje słownik formularza typu danych na typ danych string.

Python3




test1> => {> 'testname'> :> 'akshat'> ,> > 'test2name'> :> 'manjeet'> ,> > 'test3name'> :> 'nikhil'> }> # print original dictionary> print> (> type> (test1))> print> (> 'initial dictionary = '> , test1)> # convert dictionary into string> result> => str> (test1)> # print resulting string> print> (> ' '> ,> type> (result))> print> (> 'final string = '> , result)>

Wyjście:

initial dictionary = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’}  final string = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’} 

Złożoność przestrzeni: NA)
Złożoność czasowa: NA)

Konwertuj słownik na ciąg znaków, używając metody drukowania

Innym podejściem do konwersji obiektu słownika na ciąg znaków jest użycie metody print. Druk umożliwia ładne wydrukowanie dowolnego materiału Pyton struktury danych w takiej formie wydrukować może być użyte jako dane wejściowe dla interpretera.

Oto przykład użycia modułu print that do konwersji obiektu słownika na ciąg znaków:

Python3




import> pprint> # Initialize dictionary> d> => {> 'testname'> :> 'akshat'> ,> 'test2name'> :> 'manjeet'> ,> 'test3name'> :> 'nikhil'> }> # Print original dictionary> print> (f> 'Original dictionary: {d}'> )> # Convert dictionary into string using pprint.pformat()> result> => pprint.pformat(d)> # Print resulting string> print> (f> ' Resulting string: {result}'> )> print> (> 'Type is: '> ,> type> (result))>

Wyjście

Original dictionary: {'testname': 'akshat', 'test2name': 'manjeet', 'test3name': 'nikhil'} Resulting string: {'test2name': 'manjeet', 'test3name': 'nikhil', 'testname': 'akshat'} Type is:    Space complexity :   O(n)   Time complexity :   O(n) The print module provides more control over the formatting of the resulting string, such as indentation and line width, than the built-in str and json.dumps functions.