Slovník je důležitým kontejnerem a používá se téměř v každém kódu každodenního programování a také při vývoji webu Krajta . Čím více se používá, tím větší je požadavek na jejich zvládnutí, a proto je nutné se o nich učit.
Input: { 'testname' : 'akshat','test2name' : 'manjeet','test3name' : 'nikhil'} Output: {'testname': 'akshat', 'test2name': 'manjeet', 'test3name': 'nikhil'} Explanation: Input type is but the output type is> Podívejme se na různé způsoby, jak změnit slovník na řetězec.
Objekt slovníku do řetězce Konverzace
Níže jsou uvedeny metody, které pokryjeme v tomto článku:
- Použitím json.dumps() metoda
- Použitím str() funkce
- Použitím způsob tisku
Převod Dict na String v Pythonu pomocí metody json.dumps().
Zde můžeme použít metodu dump() z JSON importem, který převede datový typ slovníku na řetězec. V níže uvedeném kódu nejprve provedeme test slovníku1 a poté použijeme json.dumps a předejte v ní slovník tes1 a dostaneme požadovaný výsledek v tětiva 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ýstup:
initial dictionary = {‘testname’: ‘akshat’, ‘test2name’: ‘manjeet’, ‘test3name’: ‘nikhil’} final string = {testname: akshat, test2name: manjeet, test3name: nikhil}> Složitost prostoru: Na)
Časová složitost: Na)
Konverze slovníku na řetězec pomocí funkce str().
The str() funkce převede zadanou hodnotu na řetězec. Funkce string je také užitečná pro převod datového typu na řetězcový, tím předáme slovník do této metody a ona převede formulářový slovník datových typů na datový typ řetězec.
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ýstup:
initial dictionary = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’} final string = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’}> Složitost prostoru: Na)
Časová složitost: Na)
Převeďte slovník na řetězec pomocí metody tisku
Dalším způsobem převodu objektu slovníku na řetězec je použití tisku. Tisk poskytuje způsob, jak libovolně tisknout Krajta datové struktury ve formě, která tisk lze použít jako vstup pro tlumočníka.
Zde je příklad použití tiskového modulu k převodu objektu slovníku na řetězec:
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))> |
>
příklad
>Výstup
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.>