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
for循环中的zip函数有问题;python_Python_List_Map_For Loop_Zip - Fatal编程技术网

for循环中的zip函数有问题;python

for循环中的zip函数有问题;python,python,list,map,for-loop,zip,Python,List,Map,For Loop,Zip,在Python2.7中工作 我有两个列表(简化以使解释更清楚): 和三个功能 def exit_score(list): exit_scores = [] length = len(list) for i in range(0, length): score = list[i][2] - list[i][0] exit_scores.append(score) return exit_scores 首先,我将B的相应值附加到T中的

在Python2.7中工作

我有两个列表(简化以使解释更清楚):

和三个功能

def exit_score(list):
    exit_scores = []
    length = len(list)
    for i in range(0, length):
        score = list[i][2] - list[i][0]
        exit_scores.append(score)
    return exit_scores
首先,我将B的相应值附加到T中的相应列表中:

def Trans(T, B):
    for t, b in zip(T, B):
        t.extend(b)
    a = exit_score(T)
    b = T
    score_add(b, a)
然后,使用前面列出的exit_score函数。我从每个列表的列表[0]位置的值中减去列表[2]位置的值。然后,我将这些结果附加到另一个列表(exit_分数)

最后,我想把退出分数(现在是a)添加到原始列表中

因此,我使用我的score_add(b,a)函数,它是:

score_add(team, exit_scores):
    for t, e in zip(team, exit_scores)
        t.extend(e)
    print team
如果一切正常,我会得到如下输出:

[[1,0,1,0], [1,0,3,-2], [0,5,2,-2], [3,-1,2,1]]
相反,我得到一个类型错误,告诉我不能迭代整数。但我试过打印a和b,它们都是列表形式

当我更改代码以确保退出分数在列表中时:

def Trans(T, B):
    es = []
    for t, b in zip(T, B):
        t.extend(b)
    es.append(exit_score(T))
    score_add(T, es)
整个退出分数列表添加到第一个T列表的末尾:

[[1, 0, 1, 0, 2, 2, -1], [1, 0, 3], [0, 5, 2], [3, -1, 2]]

就我的一生而言,我不知道我做错了什么…

这次是
list.append()
,而不是
list.extend()

B
是一个列表列表,而
exit\u scores
是一个整数列表

编辑:以下是整个代码的清理版本:

for t, b in zip(T, B):
    t.extend(b)
    t.append(t[2] - t[0])

这次是
list.append()
,而不是
list.extend()

B
是一个列表列表,而
exit\u scores
是一个整数列表

编辑:以下是整个代码的清理版本:

for t, b in zip(T, B):
    t.extend(b)
    t.append(t[2] - t[0])
我要咬:

map(lambda x, y: x + y + [x[0] - y[0]], T, B)
屈服:

[[1, 0, 1, 0], [1, 0, 3, -2], [0, 5, 2, -2], [3, -1, 2, 1]]
此外,还可以在列表中完成:

[x+y+[x[0]-y[0]] for x, y in zip(T, B)]
我要咬:

map(lambda x, y: x + y + [x[0] - y[0]], T, B)
屈服:

[[1, 0, 1, 0], [1, 0, 3, -2], [0, 5, 2, -2], [3, -1, 2, 1]]
此外,还可以在列表中完成:

[x+y+[x[0]-y[0]] for x, y in zip(T, B)]

你需要重新思考所有的代码。它比一个非常简单的操作要复杂得多。你需要重新思考所有的代码。对于一个非常简单的操作来说,它比它需要的要复杂得多。没错。你知道我在哪里可以学到这些命令吗。显然,这方面有点薄弱…@BurtonGuster:我先从一些Python教程开始。办公室的教程很好,我也很喜欢。你说得对。你知道我在哪里可以学到这些命令吗。显然,这方面有点薄弱…@BurtonGuster:我先从一些Python教程开始。办公室教程很好,我也很喜欢。你复制了OP不想要的结果。你复制了OP不想要的结果。