Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将输出转换为dict?_Python - Fatal编程技术网

如何在python中将输出转换为dict?

如何在python中将输出转换为dict?,python,Python,我是Python新手,遇到了一些问题。我从端点获取响应并返回名称和文档数。其代码如下所示:- def getDetails(): req = requests.get("url").json() for name in req['collections']: collection_size = requests.get("string url" + name + "/select?q=*:*&

我是Python新手,遇到了一些问题。我从端点获取响应并返回名称和文档数。其代码如下所示:-

def getDetails(): 
    req = requests.get("url").json() 
    
    for name in req['collections']:
         collection_size = requests.get("string url" + name + "/select?q=*:*&rows=0").json()["response"]["numFound"] 
         print(name, collection_size)

getDetails()
testing1 105042
testing2 558272707
testing3 1328
testing4 1404321
testing5 2152273
testing6 253621
for key, value in getDetails():
                l = tr()
                l += td(key, width="10%")
                l += td(value, width="80%")
                
我的输出如下所示:-

def getDetails(): 
    req = requests.get("url").json() 
    
    for name in req['collections']:
         collection_size = requests.get("string url" + name + "/select?q=*:*&rows=0").json()["response"]["numFound"] 
         print(name, collection_size)

getDetails()
testing1 105042
testing2 558272707
testing3 1328
testing4 1404321
testing5 2152273
testing6 253621
for key, value in getDetails():
                l = tr()
                l += td(key, width="10%")
                l += td(value, width="80%")
                
比如说:-

def getDetails(): 
    req = requests.get("url").json() 
    
    for name in req['collections']:
         collection_size = requests.get("string url" + name + "/select?q=*:*&rows=0").json()["response"]["numFound"] 
         print(name, collection_size)

getDetails()
testing1 105042
testing2 558272707
testing3 1328
testing4 1404321
testing5 2152273
testing6 253621
for key, value in getDetails():
                l = tr()
                l += td(key, width="10%")
                l += td(value, width="80%")
                
但是如果我想把它转换成某种类型的
dict()
,这样我就可以循环这个输出并得到名称和编号,那该怎么办呢


这对我来说是一个相对较新的概念,所以请原谅任何幼稚的错误。感谢您的帮助。

这是一个非常简单的修改

file_dict = dict()
for name in req['collections']:
         collection_size = requests.get("string url" + name + "/select?q=*:*&rows=0").json()["response"]["numFound"] 
         print(name, collection_size)
         file_dict[name] = collection_size

像这样改变函数

def getDetails(): 
    req = requests.get("url").json() 
    myDict = {} # Initialize empty dict
    for name in req['collections']:
         collection_size = requests.get("string url" + name + "/select?q=*:*&rows=0").json()["response"]["numFound"] 
         myDict[name] = collection_size # add keys as name and value as collection_size, dynamically 
    
    return myDict 
然后像这样使用它:

myDetails = getDetails()

myDetails就是字典。

您的输出看起来是这样的,因为您可以直接打印这些值。使用这些值设置dict中的项目,您将拥有dict。虽然仅代码答案可能会回答问题,但您可以通过提供代码的上下文、代码工作的原因以及一些文档参考以供进一步阅读,从而显著提高答案的质量。From:“简洁是可以接受的,但更全面的解释更好。”