Python | Previesť objekt slovníka na reťazec
Slovník je dôležitý kontajner a používa sa takmer v každom kóde každodenného programovania, ako aj pri vývoji webu Python . Čím viac sa používa, tým väčšia je požiadavka na ich zvládnutie, a preto je potrebné sa o nich učiť.
Input: { 'testname' : 'akshat','test2name' : 'manjeet','test3name' : 'nikhil'} Output: {'testname': 'akshat', 'test2name': 'manjeet', 'test3name': 'nikhil'} Explanation: Input type is but the output type is Pozrime sa na rôzne spôsoby zmeny slovníka na reťazec.
Objekt slovníka do reťazca Konverzácia
Nižšie sú uvedené metódy, ktorým sa budeme venovať v tomto článku:
- Použitím json.dumps() metóda
- Použitím str() funkciu
- Použitím spôsob tlače
Konverzia Dict na String v Pythone pomocou metódy json.dumps().
Tu môžeme použiť metódu dump() z JSON knižnicu jej importovaním, čo konvertuje dátový typ slovníka na reťazec. V nižšie uvedenom kóde najprv vykonáme test slovníka1 a potom použijeme json.dumps a odovzdajte v nej slovník tes1 a dostaneme požadovaný výsledok v reťazec formát.
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)> |
Výkon:
initial dictionary = {‘testname’: ‘akshat’, ‘test2name’: ‘manjeet’, ‘test3name’: ‘nikhil’} final string = {testname: akshat, test2name: manjeet, test3name: nikhil} Zložitosť priestoru: O(n)
Časová zložitosť: O(n)
Konverzia slovníka na reťazec pomocou funkcie str().
The str() funkcia skonvertuje zadanú hodnotu na reťazec. Funkcia string je tiež užitočná na konverziu dátového typu na reťazcový typ, tým prejdeme slovník do tejto metódy a tá skonvertuje formulárový slovník dátových typov na reťazcový dátový typ.
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)> |
Výkon:
initial dictionary = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’} final string = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’} Zložitosť priestoru: O(n)
Časová zložitosť: O(n)
Preveďte slovník na reťazec pomocou metódy tlače
Ďalším prístupom k prevodu objektu slovníka na reťazec je použitie tlače. Tlač poskytuje spôsob, ako ľubovoľne pekne tlačiť Python dátové štruktúry vo forme, ktorá vytlačiť možno použiť ako vstup pre tlmočníka.
Tu je príklad použitia modulu tlače na konverziu objektu slovníka na reťazec:
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))> |
Výkon
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.