Python代码中的错误
有一个整数数组。还有不相交的集合A和B,每个集合都包含整数。你喜欢集合A中的所有整数,不喜欢集合B中的所有整数。你最初的幸福感是0。对于数组中的每一个整数,如果我在A中,你的幸福值加1。如果我在B,你会给你的幸福增加-1。否则,你的幸福不会改变。最后输出你最后的快乐 输入格式 第一行包含由空格分隔的整数n和m。 第二行包含n个整数,即数组的元素。 第三行和第四行分别包含m个整数A和B 输出格式 输出一个整数,你的总幸福 样本输入 3.2 1 5 3 3.1 5.7 样本输出 一, 有人能解释一下这个解决方案有什么问题吗?它通过了一些测试,但在其他测试中失败Python代码中的错误,python,set,Python,Set,有一个整数数组。还有不相交的集合A和B,每个集合都包含整数。你喜欢集合A中的所有整数,不喜欢集合B中的所有整数。你最初的幸福感是0。对于数组中的每一个整数,如果我在A中,你的幸福值加1。如果我在B,你会给你的幸福增加-1。否则,你的幸福不会改变。最后输出你最后的快乐 输入格式 第一行包含由空格分隔的整数n和m。 第二行包含n个整数,即数组的元素。 第三行和第四行分别包含m个整数A和B 输出格式 输出一个整数,你的总幸福 样本输入 3.2 1 5 3 3.1 5.7 样本输出 一, 有人能解释一下
input()
array = set(input().split())
set1 = set(input().split())
set2 = set(input().split())
res = len(set1 & array) - len(set2 & array)
print(res)
问题是,您正在将输入转换为集合,而集合又会删除重复项。如果你的输入中有重复的值,那么你只需要在结果中加/减1。如果这是正确的,那么代码就可以了。如果不是,那么您应该使用列表而不是集合 代码可以是这样的:
# The first part should stay the same, without the set() call on array
input()
array = input().split()
list1 = set(input().split())
list2 = set(input().split())
# Now we use some list comprehension to get the happiness result
res = sum([1 for elem in array if elem in list1]) - sum([1 for elem in array if elem in list2])
第一个和累加正的点,第二个和累加负的点。它适用于多个事件,每个事件增加/减少一个点
编辑
更清晰的方法,了解for循环
# The first part should stay the same, without the set() call on array
input()
array = input().split()
list1 = set(input().split())
list2 = set(input().split())
# We create a variable res which will store the resulting happiness, initially 0
res = 0
# Now we iterate through the elements in array and check wheter they should add or substract
for elem in array:
# If the element is in list1, we add 1 to res
if elem in list1:
res += 1
# If the element is in list2, we substract 1 from res
elif elem in list2:
res -= 1
问题是,您正在将输入转换为集合,而集合又会删除重复项。如果你的输入中有重复的值,那么你只需要在结果中加/减1。如果这是正确的,那么代码就可以了。如果不是,那么您应该使用列表而不是集合 代码可以是这样的:
# The first part should stay the same, without the set() call on array
input()
array = input().split()
list1 = set(input().split())
list2 = set(input().split())
# Now we use some list comprehension to get the happiness result
res = sum([1 for elem in array if elem in list1]) - sum([1 for elem in array if elem in list2])
第一个和累加正的点,第二个和累加负的点。它适用于多个事件,每个事件增加/减少一个点
编辑
更清晰的方法,了解for循环
# The first part should stay the same, without the set() call on array
input()
array = input().split()
list1 = set(input().split())
list2 = set(input().split())
# We create a variable res which will store the resulting happiness, initially 0
res = 0
# Now we iterate through the elements in array and check wheter they should add or substract
for elem in array:
# If the element is in list1, we add 1 to res
if elem in list1:
res += 1
# If the element is in list2, we substract 1 from res
elif elem in list2:
res -= 1
我将列表A和B的输入作为一般列表。我想通过下面的列表理解在一行中获得快乐。在我将print和“happiness=”合并到一行之后。显然,这是使代码更快的解决方案
input()
my_array = input().split()
listA=list(input().split())
listB=list(input().split())
print (sum(1 for data in my_array if data in listA)+sum(-1 for data in my_array if data in listB))
我将列表A和B的输入作为一般列表。我想通过下面的列表理解在一行中获得快乐。在我将print和“happiness=”合并到一行之后。显然,这是使代码更快的解决方案
input()
my_array = input().split()
listA=list(input().split())
listB=list(input().split())
print (sum(1 for data in my_array if data in listA)+sum(-1 for data in my_array if data in listB))
你能提供一个失败的例子以便更容易发现错误吗?可能是因为你使用的是集合:如果我输入
1 3 5
,你会说那是1
,应该是2
:数组不能简化为集合。你能提供一个失败的例子以便更容易发现错误吗?可能是因为你在使用集合:如果我放入1335
,你会说那是1
,它应该是2
:数组不能简化为一个集合。list1
和list2
可以保留集合,这样中的速度更快。此外,如果您只想在数组中迭代一次,您可以结合列表理解:求和([1 if-elem in list1 else-1 if-elem in list2 else 0 for elem in array])
,尽管我建议在实际代码中只进行分解,因为您所做的并不明显。您的是一个更好的单行程序,但您是对的,我最好把它拆了。list1
和list2
可以保持不变,这样中的速度更快。此外,如果您只想在数组中迭代一次,您可以结合列表理解:求和([1 if-elem in list1 else-1 if-elem in list2 else 0 for elem in array])
,尽管我建议在实际代码中只进行分解,因为您所做的并不明显。您的是一个更好的单行程序,但您是对的,我最好还是说出来。