在python中为forloop附加字符串

时间:2017-04-22 04:48:35

标签: python for-loop

我需要在python列表中打印并附加所有值的列表,我无法得到它。你能帮助我吗?我只是将一个系统数据与另一个系统数据进行比较,并确定每个字段的不匹配。

示例,表格为2个不同系统中的kid_id,姓名,年龄,性别,地址。我需要确保所有孩子的数据都从1data正确地移动到2data系统。 Emp_id喜欢1,2,3,4,5,6

2_data = self.get2Data(kid_id)
1_data = self.get1Data(kid_id)
for i in range(len(1data)):
    for key, value in 1data[i].items():
        if 1data[i][key] == 2data[i][key]:
            result = str("LKG") + ","+ str(kid_id) +","+ str("PASS") + "," + str(key)
        else:
            result = str("LKG") + "," + str(kid_id) + "," + str("FAIL") + "," + str(key)
        MatchResult = result.split()
        print MatchResult
print "***It is Done*****"

目前我的输出就像,

['LKG,100,PASS,address']
['LKG,102,FAIL,dob']
['LKG,105,FAIL,gender']

但我需要的方式,

(['LKG,100,PASS,address'],['LKG,102,FAIL,dob'],['LKG,105,FAIL,gender'])

or 
[('LKG,100,PASS,address'),('LKG,102,FAIL,dob'),('LKG,105,FAIL,gender')]

代码详细信息:上面的代码将比较两个系统数据,并通过打印上述格式显示通过和失败的情况。如果您看到上面的结果,那么打印地址为pass,dob为失败,性别为失败,这意味着仍然数据不匹配是针对持有102和105的孩子的dob和性别字段。

1 个答案:

答案 0 :(得分:0)

在循环之前移动列表变量声明并将其初始化为空列表,然后每次追加结果。

2_data = self.get2Data(kid_id)
1_data = self.get1Data(kid_id)
MatchResult=[]
for i in range(len(1data)):
    for key, value in 1data[i].items():
        if 1data[i][key] == 2data[i][key]:
            result = str("LKG") + ","+ str(kid_id) +","+ str("PASS") + "," + str(key)
        else:
            result = str("LKG") + "," + str(kid_id) + "," + str("FAIL") + "," + str(key)
        MatchResult.append( result.split())
print MatchResult
print "***It is Done*****"