根据第二个维度中的项目拆分两个元组的列表

时间:2016-04-03 02:59:16

标签: python dictionary classification

有一个名为 a 的列表,有两个维度。 a[0]是1000个图像的元组,a[1]是其标签的元组。我正在尝试运行以下代码:

from collections import defaultdict
images = defaultdict(list)  
for image, label in a:
    images[label].append(image)

for k,v in images.iteritems():
    print('Label {} has {} images'.format(k, len(v)))

但我收到以下错误:

for image, label in a:

ValueError: too many values to unpack

1 个答案:

答案 0 :(得分:4)

尝试将第一个循环更改为:

import com.ximpleware.*;

public class replaceContent {
    public static void main(String[] s) throws VTDException, Exception{
        VTDGen vg = new VTDGen();
        AutoPilot ap = new AutoPilot();
        XMLModifier xm = new XMLModifier();
        if (vg.parseFile("d:\\xml\\input2.xml", true)){
            VTDNav vn = vg.getNav();
            ap.bind(vn);
            xm.bind(vn);
            ap.selectXPath("/clients/function/block");
            int i=-1;
            byte[] s1 = ("\r\n\t\t<opster>hello = world</opster>\r\n\t\t"+
                        "<opster>abc = def</opster>\r\n\t").getBytes();
            while((i=ap.evalXPath())!=-1){
                xm.insertAfterHead(s1);
                long l= vn.getContentFragment(); // add new stuff after the starting tag
                xm.removeContent((int)l, (int)(l>>32)); // remove old stuff
            }
            xm.output("d:\\xml\\new.xml");
        }
        }
}

这将合并来自for image, label in zip(*a): a[0]的相应对,以便您可以遍历它们。