Python 求和可能性,一个循环

Python 求和可能性,一个循环,python,Python,早些时候,我有很多优秀的程序员帮助我完成一个函数。然而,讲师希望它在一个循环中,所有的工作解决方案都使用多个循环 我写了另一个程序,几乎解决了这个问题。与使用循环来比较所有值不同,您必须使用函数has_key来查看该特定键是否存在。这个问题的答案将使您无需通过字典查找匹配值,因为您只需知道它们是否匹配。 同样,charCount只是一个将自身常量输入字典并返回字典的函数 def sumPair(theList, n): for a, b in level5.charCount(theLi

早些时候,我有很多优秀的程序员帮助我完成一个函数。然而,讲师希望它在一个循环中,所有的工作解决方案都使用多个循环

我写了另一个程序,几乎解决了这个问题。与使用循环来比较所有值不同,您必须使用函数has_key来查看该特定键是否存在。这个问题的答案将使您无需通过字典查找匹配值,因为您只需知道它们是否匹配。 同样,charCount只是一个将自身常量输入字典并返回字典的函数

def sumPair(theList, n):
    for a, b in level5.charCount(theList).iteritems():    
        x = n - a     
        if level5.charCount(theList).get(a):
            if a == x:
                if b > 1: #this checks that the frequency of the number is greater then one so the program wouldn't try to multiply a single possibility by itself and use it (example is 6+6=12. there could be a single 6 but it will return 6+6
                    return a, x
            else:
                if level5.charCount(theList).get(a) != x:
                    return a, x           
print sumPair([6,3,8,3,2,8,3,2], 9)  
我只需要让这段代码通过查看元素列表中是否存在当前元素来查找总和,而无需迭代。

您可以使用函数而不是
level5.charCount

我不知道为什么你需要检查level5.charCount(列表).get(a):。我认为没有必要
a
是从
level5.charCount(列表)

因此,我简化了您的代码:

form collections import Counter

def sumPair(the_list, n):
    for a, b in Counter(the_list).iteritems():
        x = n - a
        if a == x and b >1:
            return a, x
        if a != x and b != x:
            return a, x

print sumPair([6, 3, 8, 3, 2, 8, 3, 2], 9)   #output>>> (8, 1)
也可以这样使用:

>>>result = [(a, n-a) for a, b in Counter(the_list).iteritems() if a==n-a and b>1 or (a != n-a and b != n-a)]
>>>print result
[(8, 1), (2, 7), (3, 6), (6, 3)]
>>>print result[0]   #this is the result you want
(8, 1)
您可以使用函数代替
level5.charCount

我不知道为什么你需要检查level5.charCount(列表).get(a):。我认为没有必要
a
是从
level5.charCount(列表)

因此,我简化了您的代码:

form collections import Counter

def sumPair(the_list, n):
    for a, b in Counter(the_list).iteritems():
        x = n - a
        if a == x and b >1:
            return a, x
        if a != x and b != x:
            return a, x

print sumPair([6, 3, 8, 3, 2, 8, 3, 2], 9)   #output>>> (8, 1)
也可以这样使用:

>>>result = [(a, n-a) for a, b in Counter(the_list).iteritems() if a==n-a and b>1 or (a != n-a and b != n-a)]
>>>print result
[(8, 1), (2, 7), (3, 6), (6, 3)]
>>>print result[0]   #this is the result you want
(8, 1)

不要使用
has_key
,它已被弃用。如果在level5.charCount(列表)中使用了
。然而,我不明白为什么这是必要的,因为你知道
a
level5.charCount(列表)
的一个键,因为你刚刚从那里得到它。那家伙说我不能在@webmaster中使用
\uuuuuuuuuuu包含
?;-)@站长:照他说的做,因为这是为了一门课,只要知道这不是现实世界中最好的方式<代码>has_key甚至在python的最新版本中都不存在。看,我完全不知道如何做到这一点。我花了超过15个小时在这个问题上。我不必使用has键或get键。但是我不能在level5.charCount(theList.iteritems():“不要使用
has_key
”中的第一个“for a,b”之后使用任何迭代,这是不推荐的。如果在level5.charCount(列表)中使用了
。然而,我不明白为什么这是必要的,因为你知道
a
level5.charCount(列表)
的一个键,因为你刚刚从那里得到它。那家伙说我不能在@webmaster中使用
\uuuuuuuuuuu包含
?;-)@站长:照他说的做,因为这是为了一门课,只要知道这不是现实世界中最好的方式<代码>has_key甚至在python的最新版本中都不存在。看,我完全不知道如何做到这一点。我花了超过15个小时在这个问题上。我不必使用has键或get键。但是我不能在level5.charCount(theList.iteritems():)中的第一个“for a,b”之后使用任何迭代,它不起作用。。输出应该是和对中的两个数字之一。所以9应该是3,并且6@WebMaster我不知道你想要什么。你能再举个例子吗?它不起作用。。输出应该是和对中的两个数字之一。所以9应该是3,并且6@WebMaster我不知道你想要什么。你能再举个例子吗?