파이썬 | 사전 객체를 문자열로 변환

사전은 중요한 컨테이너이며 웹 개발뿐만 아니라 일상적인 프로그래밍의 거의 모든 코드에서 사용됩니다. 파이썬 . 더 많이 사용할수록 그것을 숙달해야 하는 요구 사항이 더 많아지기 때문에 이에 대해 배우는 것이 필요합니다.

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

사전을 문자열로 변경하는 다양한 방법을 살펴보겠습니다.

사전 객체를 문자열 대화로 변환

이 기사에서 다룰 방법은 다음과 같습니다.

Python에서 Dict를 문자열로 변환 json.dumps() 메소드 사용

여기서는 dump() 메소드를 사용할 수 있습니다. JSON 사전 데이터 유형을 문자열로 변환하는 라이브러리를 가져옵니다. 아래 코드에서는 먼저 사전 test1을 가져온 다음 다음을 사용합니다. json.dumps 메서드를 사용하고 tes1 사전을 전달하면 다음에서 필요한 결과를 얻을 수 있습니다. 체재.

파이썬3




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)>

산출:

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

공간 복잡도 : 에)
시간 복잡도 : 에)

str() 함수를 사용하여 사전을 문자열로 변환

그만큼 str() 함수는 지정된 값을 문자열로 변환합니다. 문자열 함수는 또한 사전을 이 메소드에 전달하고 데이터 유형을 사전에서 문자열 데이터 유형으로 변환함으로써 데이터 유형을 문자열 유형으로 변환하는 데 도움이 됩니다.

파이썬3




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)>

산출:

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

공간 복잡도 : 에)
시간 복잡도 : 에)

인쇄 메소드를 사용하여 사전을 문자열로 변환

사전 객체를 문자열로 변환하는 또 다른 접근 방식은 인쇄를 사용하는 것입니다. 인쇄는 임의의 모양을 예쁘게 인쇄하는 방법을 제공합니다. 파이썬 형태의 데이터 구조 인쇄 인터프리터의 입력으로 사용될 수 있습니다.

다음은 print that 모듈을 사용하여 사전 객체를 문자열로 변환하는 예입니다:

파이썬3




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))>

산출

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.