Python 数组搜索不工作-不知道为什么?
我对python比较陌生,目前正在从事一个在线的学校项目。当我运行这段代码时,它应该计算数组中出现“Action”和“Sport”的次数Python 数组搜索不工作-不知道为什么?,python,arrays,numpy,Python,Arrays,Numpy,我对python比较陌生,目前正在从事一个在线的学校项目。当我运行这段代码时,它应该计算数组中出现“Action”和“Sport”的次数 forename = ["Joe", "George", "Oliver"] HistoryGenre=[["Action", "Action", "Action", "Action", "Sport", "Sport", "Action", "Action", "Action", "Sport"], ["Sport", "Sport", "Sport", "
forename = ["Joe", "George", "Oliver"]
HistoryGenre=[["Action", "Action", "Action", "Action", "Sport", "Sport", "Action", "Action", "Action", "Sport"], ["Sport", "Sport", "Sport", "Sport", "Action", "Action", "Sport", "Sport", "Sport", "Action"], ["Action", "Action", "Sport", "Sport", "Action", "Action", "Action", "Sport", "Sport", "Sport"]]
rr=1
ActionCounter=0
SportCounter=0
while rr==1:
rec=input("Who would you like to recommend games for?")
if rec in forename:
rr+=1
r=forename.index(rec)
RepeatIndex=0
for i in HistoryGenre[r]:
if HistoryGenre[r:RepeatIndex]=="Action":
ActionCounter+=1
RepeatIndex+=1
else:
SportCounter+=1
RepeatIndex+=1
if RepeatIndex==9:
print(ActionCounter)
print(SportCounter)
运行此代码时,ActionCounter打印为0,SportCounter打印为9。考虑到输出应该是7和3,我不明白这是怎么发生的或为什么发生的。这很可能是初学者的错误。代码的问题就在这里
if HistoryGenre[r:RepeatIndex]=="Action":
要引用HistoryGene[r]中的第n项,请执行以下操作:
HistoryGenre[r][n]
但是你在历史流派[r]上处于一个for循环中,所以你可以这样做
for i in HistoryGenre[r]:
if i == "Action":
...
我不知道给你的项目是否禁止计数功能,但它会让你的生活更轻松。不必添加到ActionCounter和SportCounter变量,您可以在每个列表中找到如下计数:
ActionCounter = HistoryGenre[r][:9].count("Action")
[:9]获取列表中的前9个元素,这似乎就是您想要的。此外,您可以使用字典,而不是两个平行列表。在字典中,元素有键,所以要引用某个元素,可以使用它的键。比如说
namebook = {"Joe":["Action", "Action", "Action", "Action", "Sport", "Sport", "Action", "Action", "Action", "Sport"], "George":["Sport", "Sport", "Sport", "Sport", "Action", "Action", "Sport", "Sport", "Sport", "Action"], "Oliver":["Action", "Action", "Sport", "Sport", "Action", "Action", "Action", "Sport", "Sport", "Sport"]}
要获得Joe的书类型,只需使用
namebook["Joe"]
使用这些更改,您的程序可以缩短很多
namebook = {"Joe":["Action", "Action", "Action", "Action", "Sport", "Sport", "Action", "Action", "Action", "Sport"], "George":["Sport", "Sport", "Sport", "Sport", "Action", "Action", "Sport", "Sport", "Sport", "Action"], "Oliver":["Action", "Action", "Sport", "Sport", "Action", "Action", "Action", "Sport", "Sport", "Sport"]}
inputname = input()
print(namebook[inputname][:9].count("Action"))
print(namebook[inputname][:9].count("Sport"))
这并不能回答您的问题,但因为这里已经回答了,所以如果您想实现这一点,我想计算每个子列表中出现
“Sports”
和“Action”
的次数
for subList in HistoryGenre:
string = ""
string += ' '.join(subList) + " "
words = {}
for word in string.split():
try:
words[word] += 1
except KeyError:
words[word] = 1
print(words)
Out:
{'Sport': 3, 'Action': 7}
{'Sport': 7, 'Action': 3}
{'Sport': 5, 'Action': 5}
word
是关键
起初,单词[word]
不存在。所以当我尝试words[word]+=1
该程序将导致一个键错误
,因为键字
不存在
这样程序就不会崩溃,除了键错误
在KeyError
块内,键word
设置为值1
因此,因为现在每当在
string.split()
中遇到单词时,word
都是一个键,1
被添加到word的值中。哇,非常感谢,这解决了我的问题。似乎我不知道搜索数组的正确语法。但是我仍然不知道为什么ActionCounter的输出是0,为什么SportCounter的输出是9。另外,谢谢你教我一些我从未使用过的字典。@OliD HistoryGene[r:RepeatIndex]=“Action”从来都不是真的,所以它总是会跳过它,转到else:SportsCounter+=1。这真的会帮助我更好地理解它。但是'except KeyError:行做什么呢?显然,我对很多Python都不太熟悉。