} } }

    python并行迭代

    添加时间:2013-6-26 点击量:

    并行迭代:同时并行遍历两个列表


    for line1,line2 in zip(line1_list, line2_list):


        ...


    无聊,贴一段刚才的代码:



     1 import sys
    
    2 import string
    3
    4 expected=
    5 result=
    6
    7 if sys.argv[1] == --help:
    8 print example: --expected=expected --result=result
    9 if len(sys.argv) < 3:
    10 print please provide correct file path
    11 sys.exit()
    12
    13 index = 1
    14 while index < 3:
    15 if sys.argv[index].startswith(--expected):
    16 expected = sys.argv[index][11:]
    17 elif sys.argv[index].startswith(--result):
    18 result = sys.argv[index][9:]
    19 index += 1
    20
    21 expected_input = open(expected, r
    22 result_input = open(result, r
    23 expected_lines = expected_input.readlines()
    24 result_lines = result_input.readlines()
    25 expected_input.close()
    26 result_input.close()
    27
    28 correct = 0
    29 wrong = 0
    30
    31 if len(expected_lines) != len(result_lines):
    32 print line counts are not consistent
    33 else:
    34 for exp, res in zip(expected_lines, result_lines):
    35 if exp == res:
    36 correct += 1
    37 else:
    38 wrong += 1
    39
    40 accuracy = float(correct) / (correct + wrong)
    41 print All: + str(correct + wrong) + Correct: + str(correct);
    42 print Accuracy = + str(accuracy)


    彼此相爱,却不要让爱成了束缚:不如让它成为涌动的大海,两岸乃是你们的灵魂。互斟满杯,却不要同饮一杯。相赠面包,却不要共食一个。一起歌舞欢喜,却依然各自独立,相互交心,却不是让对方收藏。因为唯有生命之手,方能收容你们的心。站在一起却不要过于靠近。—— 纪伯伦《先知》
    分享到: