Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果一个变量有两种可能的结果,如何分别从列表中添加值_Python_List_Python 3.x_Sum - Fatal编程技术网

Python 如果一个变量有两种可能的结果,如何分别从列表中添加值

Python 如果一个变量有两种可能的结果,如何分别从列表中添加值,python,list,python-3.x,sum,Python,List,Python 3.x,Sum,此赋值调用另一个函数: def getPoints(n): n = (n-1) % 13 + 1 if n == 1: return [1] + [11] if 2 <= n <= 10: return [n] if 11 <= n <= 13: return [10] 但是问题是整数1有两个可能的点值,1或11。当我计算点的和时,它做的一切都是正确的,但是它把1和11加在一起,而如果整数

此赋值调用另一个函数:

def getPoints(n):
    n = (n-1) % 13 + 1
    if n == 1:
        return [1] + [11]
    if 2 <= n <= 10:
        return [n]
    if 11 <= n <= 13:
        return [10] 
但是问题是整数1有两个可能的点值,1或11。当我计算点的和时,它做的一切都是正确的,但是它把1和11加在一起,而如果整数是1,如果整数是11,我需要它来计算和

例如:

>>>getPointTotal([1,26, 12]) # 10-13 are worth 10 points( and every 13th number that equates to 10-13 using n % 13.
>>>[21,31] # 21 if the value is 1, 31 if the value is 11.
>>>getPointTotal([1,14]) # 14 is just 14 % 13 = 1 so, 1 and 1.
>>>[2, 12, 22] # 1+1=2, 1+11=12, 11+11=22
另一个例子:

>>>getPointTotal([1,26, 12]) # 10-13 are worth 10 points( and every 13th number that equates to 10-13 using n % 13.
>>>[21,31] # 21 if the value is 1, 31 if the value is 11.
>>>getPointTotal([1,14]) # 14 is just 14 % 13 = 1 so, 1 and 1.
>>>[2, 12, 22] # 1+1=2, 1+11=12, 11+11=22
我的输出是:

>>>getPointTotal([1,14])
>>>[24] #It's adding all of the numbers 1+1+11+11 = 24.

所以我的问题是,我如何让它分别加上值1和值11,反之亦然。因此,当我有1时,它将添加所有值和1,或者它将添加所有值和11。

存储从
getPoints()
返回的所有值是一个错误。您应该只存储到目前为止返回的分数的可能总数。您可以将所有这些存储在一个集合中,并使用从
getPoints()
返回的所有可能值更新它们。集合将自动删除重复分数,例如1+11和11+1。可以在末尾将集合更改为排序列表。这是我的密码:

def getPointTotal(aList):
    totals = {0}
    for i in aList:
        totals = {p + t for p in getPoints(i) for t in totals}
    return sorted(list(totals))
我得到以下结果:

>>> print(getPointTotal([1,26, 12]))
[21, 31]
>>> print(getPointTotal([1,14]))
[2, 12, 22]

Rory Daulton的答案很好,它有效地为您提供了可能的不同总数。我想提供另一种方法,它不一定比那一种更好,只是有点不同。我的方法的好处是,您可以看到导致给定总数的分数顺序,而不仅仅是最后的总数

def getPointTotal(cards):
    card_scores = [getPoints(card) for card in cards] # will be a list of lists
    combos = {sorted(scores) for scores in itertools.product(*card_scores)}
    return [(scores, sum(scores)) for scores in combos]
此代码的关键部分是调用
itertools.product(*card\u scores)
。这将获取您从
getPoints
中获得的输入列表中每个卡的列表,并获取所有组合。所以
产品([1,11],[1,11],[10])
将给出
(1,1,10)
(1,11,10)
(11,1,10)
,和
(11,11,10)


对于21点计分来说,这可能有点过火了,因为对于给定的一组卡片,分数不会有太多变化。但是对于不同的问题(即
getPoints
函数的不同实现),它可能非常有趣。

这个问题并不清楚。1) 请显示
getPoints()
的代码或简化版本。不清楚它是如何工作的,甚至不清楚它是否返回一个整数或包含一个整数的列表。2) 您的行
total=sum(points)
应该排在
for
行下,或者
total
的评估频率将超过需要。3) 输出与您给出的示例输出不匹配。请删除
total
行,以便在循环后只计算一次。既然这是家庭作业,我就给你一个提示。看看正确答案的模式。在[1,14]中,有两个值相当于1或11(1+10)分。可能的总数是2,2+10,2+20。@RoryDaulton我添加了被调用函数的代码。@TerryJanReedy我删除了总数行,但仍然计算出与我得到的总数相同的24。所以它仍然在加1+1+11+11。我一直在思考如何将计算结果从1+1、1+11和11+11中分离出来。你想要的结果是什么?如果我运行
getPoints(1)
我会得到
[1,11]
。这是你单独想要的吗?我看不出有任何理由去做
newtotals=newtotals{p+t}
而不是
newtotals.add(p+t)
。虽然对于一个新的Python学生来说可能不太清楚,但您可以更进一步,使用集合理解来替换两个内部循环(并完全取消
newtotals
):
for i in-alist:totals={p+t for p in-getpoints(i)for t in-totals}
@Blckknght:我刚完成修改后的代码,就看到了你的评论。这恰好是你推荐的——但我真的没有使用你的评论!当然,你显然有个好主意。我在第一个版本中使用了
|
操作符,因为没有提到
add()
方法,我忘记了。你是救命恩人,我一整天都在强调这一点。