Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.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 通读字典查找int_Python_Json_Dictionary_Typeerror - Fatal编程技术网

Python 通读字典查找int

Python 通读字典查找int,python,json,dictionary,typeerror,Python,Json,Dictionary,Typeerror,我的代码有点问题。当我试着运行它时,我总是收到一条错误消息,我不知道为什么 有人能帮忙吗 import requests import argparse import json from urllib.request import urlopen url = "http://hn.algolia.com/api/v1/search_by_date?tags=story&numericFilters=created_at_i>1488196800,created_at_i<

我的代码有点问题。当我试着运行它时,我总是收到一条错误消息,我不知道为什么

有人能帮忙吗

import requests
import argparse
import json
from urllib.request import urlopen


url = "http://hn.algolia.com/api/v1/search_by_date?tags=story&numericFilters=created_at_i>1488196800,created_at_i<1488715200"
r = urlopen(url)
data = r.read().decode("utf-8")
j_data = json.loads(data)

def build_structure(data, d=[]):
    if 'hits' in data:
        for t in data['hits']:
            d.append({'title' : t.get('title'), 'point' : t.get('points')})
            build_structure(t,d)
    return d

j = build_structure(j_data)
print(j)
word = "Python"
points = "2"
def structure_search_by_title(data, word, s=[]):
    for c in data:
        if word in c['title']:
            s.append({'title' : c.get('title')})
    return s

def structure_search_by_points(data, points, s=[]):
    for c in data:
        if points in c['point']:
            s.append({'Title of the article is' : c.get('title')})


k = structure_search_by_title(j, word)
l = structure_search_by_points(j, points)
print(k)
print(l)
导入请求
导入argparse
导入json
从urllib.request导入urlopen

url=”http://hn.algolia.com/api/v1/search_by_date?tags=story&numericFilters=created_at_i>1488196800,在API返回的JSON中创建,
points
是一个整数,而不是字符串,因此
c['point']
是一个整数。因此,编写以下内容没有任何意义:

if points in c['point']:
因为
c['point']
不是您可以搜索的列表或字符串。您应该使用简单的相等性测试

def structure_search_by_points(data, points, s=[]):
    points = int(points)
    for c in data:
        if points == c['point']:
            s.append({'Title of the article is' : c.get('title')})
    return s

在API返回的JSON中,
points
是一个整数,而不是字符串,因此
c['point']
是一个整数

if points in c['point']:
因为
c['point']
不是您可以搜索的列表或字符串。您应该使用简单的相等性测试

def structure_search_by_points(data, points, s=[]):
    points = int(points)
    for c in data:
        if points == c['point']:
            s.append({'Title of the article is' : c.get('title')})
    return s

以下是有关代码的一些注释:

import requests
import argparse
import json
from urllib.request import urlopen


url = "http://hn.algolia.com/api/v1/search_by_date?tags=story&numericFilters=created_at_i>1488196800,created_at_i<1488715200"
r = urlopen(url)
data = r.read().decode("utf-8")
j_data = json.loads(data)

def build_structure(data, d=[]):   # using a mutable as an initialiser
                                   # are you sure you know what you're doing?
    if 'hits' in data:
        for t in data['hits']:
            d.append({'title' : t.get('title'), 'point' : t.get('points')})
            build_structure(t,d)   # are you sure you want to call the
                                   # function recursively? (is the key
                                   # "hits" expected to occur in data["hits"]?
    return d

j = build_structure(j_data)
print(j)
word = "Python"
points = "2"
def structure_search_by_title(data, word, s=[]): # again, a mutable initialiser
    for c in data:
        if word in c['title']:
            s.append({'title' : c.get('title')}) # can use c['title'] here since
                                                 # you know 'title' is in c
    return s

def structure_search_by_points(data, points, s=[]): # another mutable
                                                    # initialiser
    for c in data:
        if points in c['point']:      # according to the error message
                                      # c['point'] is a single number
                                      # but 'in' expects something it can
                                      # search through, like a list
                                      # on top of that you points is a string
                                      # so the two cannot be compared directly
            s.append({'Title of the article is' : c.get('title')})


k = structure_search_by_title(j, word)
l = structure_search_by_points(j, points)
print(k)
print(l)
导入请求
导入argparse
导入json
从urllib.request导入urlopen

url=”http://hn.algolia.com/api/v1/search_by_date?tags=story&numericFilters=created_at_i>1488196800,创建于_i以下是代码的一些注释:

import requests
import argparse
import json
from urllib.request import urlopen


url = "http://hn.algolia.com/api/v1/search_by_date?tags=story&numericFilters=created_at_i>1488196800,created_at_i<1488715200"
r = urlopen(url)
data = r.read().decode("utf-8")
j_data = json.loads(data)

def build_structure(data, d=[]):   # using a mutable as an initialiser
                                   # are you sure you know what you're doing?
    if 'hits' in data:
        for t in data['hits']:
            d.append({'title' : t.get('title'), 'point' : t.get('points')})
            build_structure(t,d)   # are you sure you want to call the
                                   # function recursively? (is the key
                                   # "hits" expected to occur in data["hits"]?
    return d

j = build_structure(j_data)
print(j)
word = "Python"
points = "2"
def structure_search_by_title(data, word, s=[]): # again, a mutable initialiser
    for c in data:
        if word in c['title']:
            s.append({'title' : c.get('title')}) # can use c['title'] here since
                                                 # you know 'title' is in c
    return s

def structure_search_by_points(data, points, s=[]): # another mutable
                                                    # initialiser
    for c in data:
        if points in c['point']:      # according to the error message
                                      # c['point'] is a single number
                                      # but 'in' expects something it can
                                      # search through, like a list
                                      # on top of that you points is a string
                                      # so the two cannot be compared directly
            s.append({'Title of the article is' : c.get('title')})


k = structure_search_by_title(j, word)
l = structure_search_by_points(j, points)
print(k)
print(l)
导入请求
导入argparse
导入json
从urllib.request导入urlopen

url="http://hn.algolia.com/api/v1/search_by_date?tags=story&numericFilters=created_at_i>1488196800,在_iYou创建的_应为变量提供更有意义的名称。它使调试程序变得更容易。你应该给变量起更多有意义的名字。这使调试程序变得容易多了。这不起作用,我仍然收到相同的错误消息。我不明白这是怎么回事。你确定你保存了更改吗?我知道我做错了什么,谢谢你的帮助!这不起作用,我仍然收到相同的错误消息,我不明白这是怎么回事。你确定你保存了更改吗?我知道我做错了什么,谢谢你的帮助!我为更干净的代码做了这些初始化,这样任何程序员都可以浏览并理解他们在做什么。至于带有['hits']的行,我从json解析中得到的原始字典是嵌套的,为了从嵌套字典中找到所有数据,我必须使其递归,以便找到隐藏的数据。我只是在查找隐藏数据中的整数时遇到了麻烦,我一生都无法弄清楚如何让它读取排序数组j并返回与点值匹配的标题值。@KRose我不确定您是否完全理解可变初始值设定项的问题(如果您忽略了这一点),重点是可变的。如果您两次调用这样的函数,并且它修改了变量,那么变量将不会被重置,因此,例如,如果您在第一次调用期间填充
s=[]
,那么第二次调用时变量将不会为空。更糟糕的是,由于您返回的是s,因此两个调用者将接收相同的对象。因此,如果第一个调用者对
s
做了一些事情,那么第二个调用者的
s
也会发生变化。我为更干净的代码做了一些初始化,这样任何程序员都可以通过它了解他们在做什么。至于带有['hits']的行,我从json解析中得到的原始字典是嵌套的,为了从嵌套字典中找到所有数据,我必须使其递归,以便找到隐藏的数据。我只是在查找隐藏数据中的整数时遇到了麻烦,我一生都无法弄清楚如何让它读取排序数组j并返回与点值匹配的标题值。@KRose我不确定您是否完全理解可变初始值设定项的问题(如果您忽略了这一点),重点是可变的。如果您两次调用这样的函数,并且它修改了变量,那么变量将不会被重置,因此,例如,如果您在第一次调用期间填充
s=[]
,那么第二次调用时变量将不会为空。更糟糕的是,由于您返回的是s,因此两个调用者将接收相同的对象。因此,如果第一个调用者对
s
做了一些事情,那么第二个调用者的
s
也会改变。