我对python比较陌生,目前在网上找到一个学校项目。当我运行此代码时,它应该计算“Action”和“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.这很可能是初学者的错误。< / p>
答案 0 :(得分:1)
您的代码所在的问题在这里
if HistoryGenre[r:RepeatIndex]=="Action":
要引用HistoryGenre [r]中的第n项,请执行
HistoryGenre[r][n]
但是你正在使用HistoryGenre [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"))
答案 1 :(得分:0)
这不能回答您的问题,但由于此处已经回答,我想出的是如果您计算每个子列表中出现"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
是关键
起初words[word]
不存在。所以当我尝试words[word] += 1
时
该程序将导致KeyError
,因为密钥word
不存在。
因此该程序不会导致except
KeyError
KeyError
在word
阻止内,密钥1
设置为值word
。
因为string.split()
每次在1
中遇到该词时都会成为关键词,word
会被添加到 var client = sdk.getAppAuthClient('enterprise', ENTERPRISE_ID);
//filter_term == admin to share the folder with
client.enterprise.getUsers({filter_term: 'ken.domen@nike.com'}, function(err, users) {
var userId = users.entries[0].id;
client.folders.create('0', 'New Folder', function(err, newFolder) {
client.collaborations.createWithUserID(userId, newFolder.id, client.collaborationRoles.VIEWER, function(err, collaboration) {
console.log(err);
var fileData = fs.createReadStream('/users/kdomen/Downloads/test.txt')
client.files.uploadFile(newFolder.id, 'test.txt', fileData, function(err, file) {
if (err){
console.log('err: ' + err);
}
else{
console.log('file uploaded: ' + file);
}
});
});
});
});
的值