Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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_Python - Fatal编程技术网

如何计算特定条目的编号。python

如何计算特定条目的编号。python,python,Python,我正试图数一数“elite”键,即“elite”键不为空的条目数。然后是“精英”用户的总数 以下是应具有“1”的示例数据集,因为在3个elite条目中有“1”条目不是空列表 {"yelping_since": "2013-07", "votes": {"funny": 1, "useful": 2, "cool": 2}, "review_count": 5, "name": "Kikki", "user_id": "KW35l9DVkPNJCgApafhE8w", "friends": [],

我正试图数一数“elite”键,即“elite”键不为空的条目数。然后是“精英”用户的总数

以下是应具有“1”的示例数据集,因为在3个elite条目中有“1”条目不是空列表

{"yelping_since": "2013-07", "votes": {"funny": 1, "useful": 2, "cool": 2}, "review_count": 5, "name": "Kikki", "user_id": "KW35l9DVkPNJCgApafhE8w", "friends": [], "fans": 0, "average_stars": 3.67, "type": "user", "compliments": {}, "elite": [2012]}
{"yelping_since": "2013-10", "votes": {"funny": 0, "useful": 0, "cool": 0}, "review_count": 1, "name": "Cindy", "user_id": "OdajpYB_nAJXNLSNbyMvxg", "friends": [], "fans": 0, "average_stars": 5.0, "type": "user", "compliments": {}, "elite": []}
{"yelping_since": "2013-10", "votes": {"funny": 0, "useful": 0, "cool": 0}, "review_count": 3, "name": "Lawrence", "user_id": "z-0l9wtrlGBSyMlj4BP9Lw", "friends": [], "fans": 0, "average_stars": 3.5, "type": "user", "compliments": {}, "elite": []}
下面是我尝试过的代码

import json

def count_number_of_elite():

    with open("data/yelp_academic_dataset_user.txt", "r") as input_file:
        for line in input_file:
            if json.loads(line)["elite"]:
                number_of_elite_user += 1

print (number_of_elite_user)

因为您的输入数据是json格式的,所以我将使用

import json

file = open("data/yelp_academic_dataset_checkin", "r")

for line in file:
    if json.loads(line)["elite"] != []:
        number_of_elite_user += 1
        return number_of_elite_user
此外,return语句可能不在for循环中,否则它将始终返回1

除了@UnholySheep在下面的评论中建议的改进之外,该代码的正确方式是:

import json

def count_lines():
    number_of_elite_user = 0
    with open("data/yelp_academic_dataset_checkin", "r") as input_file:
        for line in input_file:
            if json.loads(line)["elite"]:
                number_of_elite_user += 1
    return number_of_elite_user

print(count_lines())

因为您的输入数据是json格式的,所以我将使用

import json

file = open("data/yelp_academic_dataset_checkin", "r")

for line in file:
    if json.loads(line)["elite"] != []:
        number_of_elite_user += 1
        return number_of_elite_user
此外,return语句可能不在for循环中,否则它将始终返回1

除了@UnholySheep在下面的评论中建议的改进之外,该代码的正确方式是:

import json

def count_lines():
    number_of_elite_user = 0
    with open("data/yelp_academic_dataset_checkin", "r") as input_file:
        for line in input_file:
            if json.loads(line)["elite"]:
                number_of_elite_user += 1
    return number_of_elite_user

print(count_lines())

下面的代码符合您的要求:

首先确保导入json:

import json
并将函数体更改为:

file = open("data/yelp_academic_dataset_checkin", "r")

number_of_elite_user = 0

for line in file:
    user = json.loads(line)
    if (len(user['elite'])>0):
        number_of_elite_user += 1

return number_of_elite_user

下面的代码符合您的要求:

首先确保导入json:

import json
并将函数体更改为:

file = open("data/yelp_academic_dataset_checkin", "r")

number_of_elite_user = 0

for line in file:
    user = json.loads(line)
    if (len(user['elite'])>0):
        number_of_elite_user += 1

return number_of_elite_user

!=[]
是不必要的-在Python中,空列表的计算结果为
False
。另外,最好使用
和open()作为
打开文件。而且
文件
是个坏名字,因为它隐藏了一个内置函数。你完全正确。我刚刚复制了OP在问题中包含的代码,并做了一些小更改,以便问题得以解决。我已将您建议的更改包括在答案中。我尝试了您的代码@表,但仍然不起作用。它没有产生任何输出。输出应该是1。因为2012年有1名精英参赛。另外两个只是一个空列表。我在问题中重新发布了整个代码。您提供的代码不会产生任何输出。可能您在打印时输入了错误的
return
[]是不必要的-在Python中,空列表的计算结果为
False
。另外,最好使用
和open()作为
打开文件。而且
文件
是个坏名字,因为它隐藏了一个内置函数。你完全正确。我刚刚复制了OP在问题中包含的代码,并做了一些小更改,以便问题得以解决。我已将您建议的更改包括在答案中。我尝试了您的代码@表,但仍然不起作用。它没有产生任何输出。输出应该是1。因为2012年有1名精英参赛。另外两个只是一个空列表。我在问题中重新发布了整个代码。您提供的代码不会产生任何输出。可能您为
print
输入了错误的
return
。您提供的代码无效。另外,您从未执行过函数,那么为什么会期望发生任何事情?您打算做什么?学习如何编写有效的Python代码?老实说,你把基本知识弄错了,所以你应该在这一点上查阅教程。不工作,请帮助我。您提供的代码不是有效的Python。另外,您从未执行过函数,那么为什么会期望发生任何事情?您打算做什么?学习如何编写有效的Python代码?老实说,你把基本知识弄错了,所以你应该在这一点上查阅教程。不工作,请帮助我是不可接受的。