简体   繁体   English

通过 function 迭代列表继续返回 Nonetype

[英]Iterate list through a function keep returning Nonetype

So I was trying to iterate this list techno_list1 = ["C", "C#", "C++", "Java", "JavaScript", "Python", "Scala", "Oracle", "SQL Server", "MySQL Server", "PostgreSQL", "MongoDB"] through a function that return the number of job related to the keyword inside the list below is the function:所以我试图迭代这个列表techno_list1 = ["C", "C#", "C++", "Java", "JavaScript", "Python", "Scala", "Oracle", "SQL Server", "MySQL Server", "PostgreSQL", "MongoDB"]通过一个 function 返回与下面列表中的关键字相关的作业数是 function:

 def get_number_of_jobs(technology): number_of_jobs = 0 page=0 new_results=1 while new_results>0: paras={"description":technology,"page":page} r=requests.get(baseurl,paras) new_results=len(r.json()) page+=1 number_of_jobs+=(len(r.json())) return technology,number_of_jobs

It return a tuple like this ('C', 239) ('C#', 50) ('C++', 32) ('Java', 138) ('JavaScript', 116) ('Python', 98) ('Scala', 94) ('Oracle', 18) ('SQL Server', 28) ('MySQL Server', 14) ('PostgreSQL', 22) ('MongoDB', 14) So I thought it should be a tuple but when checking using type() it keep returning <class 'NoneType'>它返回一个像这样的元组('C', 239) ('C#', 50) ('C++', 32) ('Java', 138) ('JavaScript', 116) ('Python', 98) ('Scala', 94) ('Oracle', 18) ('SQL Server', 28) ('MySQL Server', 14) ('PostgreSQL', 22) ('MongoDB', 14)所以我认为应该是一个元组但是当使用 type() 检查时,它会一直返回<class 'NoneType'>

I want it to be either List or Tuple is there anyway to change the data type?我希望它是 List 或 Tuple 无论如何都可以更改数据类型?

Ok here is what I encountered:好的,这是我遇到的:

 techno_list1 = ["C", "C#", "C++", "Java", "JavaScript", "Python", "Scala", "Oracle", "SQL Server", "MySQL Server", "PostgreSQL", "MongoDB"] def number_of_each(techno_list): for x in techno_list: print(get_number_of_jobs(x)) print(type(number_of_each(techno_list1)))

Then the result:然后结果:

 ('C', 239) ('C#', 50) ('C++', 32) ('Java', 138) ('JavaScript', 116) ('Python', 98) ('Scala', 94) ('Oracle', 18) ('SQL Server', 28) ('MySQL Server', 14) ('PostgreSQL', 22) ('MongoDB', 14) <class 'NoneType'>

Maybe it a bug?也许这是一个错误?

From you extended elaboration, I can see that you are printing the type of number_of_each(techno_list1) .从您的详细说明中,我可以看到您正在打印number_of_each(techno_list1)的类型。

However, this function is currently not returning anything, it simply just prints out the values you obtained through get_number_of_jobs() .然而,这个 function 目前没有返回任何东西,它只是打印出你通过get_number_of_jobs()获得的值。 This is why it is returning <class 'NoneType'> .这就是它返回<class 'NoneType'>

I would create a list outside of the function number_of_each() and then add the items to the list to print out later, try this:我会在 function number_of_each()之外创建一个列表,然后将项目添加到列表中以便稍后打印出来,试试这个:

 techno_list1 = ["C", "C#", "C++", "Java", "JavaScript", "Python", "Scala", "Oracle", "SQL Server", "MySQL Server", "PostgreSQL", "MongoDB"] job_numb = [] def number_of_each(techno_list): for x in techno_list: job_numb.append(get_number_of_jobs(x)) print(type(get_number_of_jobs(x)) print(job_numb)

The type being printed now should be <class 'list'> for job_numb and <class 'tuple'> for the return of get_number_of_jobs() or any items in job_numb .现在打印的类型应该是<class 'list'> for job_numb<class 'tuple'> for return get_number_of_jobs()job_numb中的任何项目。

Realistically, this does not really change anything.实际上,这并没有真正改变任何事情。 You already have a tuple returned by the get_number_of_jobs() function.您已经有一个由get_number_of_jobs() function 返回的元组。 You are just misprinting the right variable.您只是错误地打印了正确的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM