“Utwórz słownik z Python String” Kod odpowiedzi

Python tworząc dyktę ze sznurka

# Python3 code to demonstrate
# convert dictionary string to dictionary
# using json.loads()
import json
  
# initializing string 
test_string = '{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}' 
  
# printing original string 
print("The original string : " + str(test_string))
  
# using json.loads()
# convert dictionary string to dictionary
res = json.loads(test_string)
  
# print result
print("The converted dictionary : " + str(res))
Bastien Chatelain

Utwórz słownik z Python String

# Python3 code to convert 
# a string to a dictionary
  
# Initializing String 
string = "{'A':13, 'B':14, 'C':15}"
  
# eval() convert string to dictionary
Dict = eval(string)
print(Dict)
print(Dict['A'])
print(Dict['C'])
dkb234

Konwertuj DICT na Python String

my_dict = {"Hello" : True, "World" : False}
print(str(my_dict))
TheProgrammer

Python tworząc dyktę ze sznurka

# Python3 code to demonstrate
# convert dictionary string to dictionary
# using ast.literal_eval()
import ast
  
# initializing string 
test_string = '{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}' 
  
# printing original string 
print("The original string : " + str(test_string))
  
# using ast.literal_eval()
# convert dictionary string to dictionary
res = ast.literal_eval(test_string)
  
# print result
print("The converted dictionary : " + str(res))
SuperPro Cz

Odpowiedzi podobne do “Utwórz słownik z Python String”

Pytania podobne do “Utwórz słownik z Python String”

Więcej pokrewnych odpowiedzi na “Utwórz słownik z Python String” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu