要求说明:

返回值是这一串:[{"negativeListCount": 1076.0, "year":"2017"}, {"negativeListCount": null, "year":"2016"}, {"negativeListCount": 2076.0, "year":"2015"}],统计有多少个{}

类型是:type 'list'

1.     详细代码:

文件名:list_num.py

# -*- coding: utf-8 -*-

import requests

import json

class list_num(object):

def __init__(self):

def list_num(self,path_data):

#print (type(path_data))  #查看类型

#num=json.loads(path_data)  #转换json格式

#print (len(path_data))

return len(path_data)

2.     Robot引用:

3.     参见问题:

(1)、python脚本,引入之后,调用常量是ok的,但调用变量就报错了,类型不匹配TypeError: expected string or buffer

代码如下:

# -*- coding: utf-8 -*-

import requests

import json

class list_num(object):

def __init__(self):

def list_num(self,path_data):

num=json.loads(path_data)

print (len(num))

return len(num)

(2)、检查变量类型

修改代码,查看输出的类型

# -*- coding: utf-8 -*-

import requests

import json

class list_num(object):

def __init__(self):

def list_num(self,path_data):

print(type(path_data)) # 查看类型

num=json.loads(path_data)

print (len(num))

return len(num)

执行结果如下:

常量是type 'unicode'类型;而变量是type 'list'类型,结果代码还用json.loads(path_data)去转type 'unicode'类型,所以才报TypeError:expected string or buffer错误

(3)、把json.loads(path_data)注释掉即可

# -*- coding: utf-8 -*-

import requests

import json

class list_num(object):

def __init__(self):

def list_num(self,path_data):

#print (type(path_data))  #查看类型

#num=json.loads(path_data)

print (len(path_data))

return len(path_data)

要求说明:返回值是这一串:[{"negativeListCount": 1076.0, "year":"2017"}, {"negativeListCount": null, "year":"2016"}, {"negativeListCount": 2076.0, "year":"2015"}],统计有多少个{}类型是:type path = "/content/data/" # . json 文件目录 file_ list = list dir(path) # 所有的. json 文件 file_index = [] # 准备存放文件索引,为了按顺序读文件 for i in range(len(file_
Python 统计 list 不同元素 个数 的方法 统计 list 元素 个数 ,这里提供一个简单的方法,使用Counter(), 示例如下: from collections import Counter # 随便定义一个 list ,也可以是自己生成的 temp = [1, 2, 1, 3, 4, 2, 3, 6, 3] count = Counter(temp) print(count) # 输出元素3的 个数 print(count[3]) # 输出结果为 Counter({1: 2, 2: 2, 3: 3,
l = [1,3,4,7,3,2,6,9,5,0,3,6,1,6,3,8,6,7,2,5] #item_id_ list : 12万 def counter(item_id_ list ): return Counter(item_id_ list ) def count(item_id_ list ): dict_item = {} for item in item_id_ list :
List 是一个集合,里面放的全是相同的 Type 据或对象。当不知道需要存放多少 据,此时用 组不太方便,new Type ary[Size] Size的大小无法确定!此时用 List 集合比较方便!此处还体现不出动态排序接口的动态所在,下面见证“动态”。 先记住一句话:几个比较方式,定义几种类!顺序和倒序算2种(我们不难发现,Student类有好几个字段都可以进行比较,比如:StuName, Age等等)
可以使用 ` type ()` 函 来判断一个变量的 类型 ,而判断一个 类型 是否为 ` list ` 类型 ,则可以使用 `isinstance()` 函 。 示例代码: ``` python # 判断变量 类型 var = [1, 2, 3] print( type (var)) # <class ' list '> # 判断 类型 是否为 list print(isinstance(var, list )) # True 在上述示例 ,首先使用 ` type ()` 函 获取 变量 `var` 的 类型 ,结果为 `<class ' list '>`,说明它是一个 ` list ` 类型 。接着使用 `isinstance()` 函 判断 `var` 是否为 ` list ` 类型 ,结果为 `True`,说明 `var` 是一个 ` list ` 类型 的变量。