Python计数2变量,如果唯一

时间:2015-06-10 08:49:02

标签: python python-2.7

如果2变量是唯一的,则代码必须对其进行计数 但我无法让它发挥作用 有人知道如何让它发挥作用吗?

我希望输出为:

C603 100nF 8

这是.txt文件的一个小例子 BaseBoard_V1.2_Componentlist.txt

C1                      1nF            C0603                 rcl                                    (24.7 35.9)           R270
C2                      100nF          C0603                 rcl                                    (36.7 32.7)           R180
C3                      10uF_Tantalum  C0603                 rcl                                    (22.7 6.45)           R0
C4                      22uF           C0603                 rcl                                    (25 8.25)             R90
C5                      1uF            C0603                 rcl                                    (22.6 21.85)          R180

代码:

from operator import itemgetter
from collections import Counter

elements = []

elements.append([])
elements.append([])
elements.append([])

with open('C:\\Python\\Artinis\\BaseBoard_V1.2_Componentslist.txt') as f:
    for i in xrange(10):
        f.next()
    for line in f:
        list = line.split();
        elements[0].append(list[0])
        elements[1].append(list[1])
        elements[2].append(list[2])

for value, package in sorted(zip(elements[1], elements[2])):
    input = value, package
    c = Counter( input )
    print ( c.items() )

输出:

[('0.22uF', 1), ('C0603', 1)]
[('100', 1), ('R0603', 1)]
[('100', 1), ('R0603', 1)]
[('100', 1), ('R0603', 1)]
[('100', 1), ('R0603', 1)]
[('C0603', 1), ('100nF', 1)]
[('C0603', 1), ('100nF', 1)]
[('C0603', 1), ('100nF', 1)]
[('C0603', 1), ('100nF', 1)]
[('C0603', 1), ('100nF', 1)]
[('C0603', 1), ('100nF', 1)]
[('C0603', 1), ('100nF', 1)]
[('C0603', 1), ('100nF', 1)]
[('10K', 1), ('R0603', 1)]
[('10K', 1), ('R0603', 1)]
[('10k', 1), ('R0603', 1)]
[('10uF_Tantalum', 1), ('C0603', 1)]
[('R0603', 1), ('19.6K', 1)]
[('1nF', 1), ('C0603', 1)]
[('C0603', 1), ('1uF', 1)]
[('2.2uF', 1), ('C0603', 1)]
[('2.2uF', 1), ('C0603', 1)]
[('R0603', 1), ('22K', 1)]
[('R0603', 1), ('22K', 1)]
[('22uF', 1), ('C0603', 1)]
[('R0603', 1), ('483', 1)]
[('53047-05', 2)]
[('ATMEGA32L-8MU', 1), ('QFN50P700X700X100-45N', 1)]

我尝试在谷歌搜索,我尝试了其他代码,但它不适合我。 有人知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

如果要将第二个和第三个元素中的每个元素计数为刚拆分的单个元素,请使用itertools.islice提取第二个和第三个元素,并在islice对象上调用元组,使用生成器表达式将其传递给Counter。 :

from collections import Counter
from itertools import islice

with open('test.txt') as f:
    print(Counter(tuple(islice(line.split(), 1, 3)) for line in f))

示例输出:

Counter({('10uF_Tantalum', 'C0603'): 1, ('22uF', 'C0603'): 1, ('1nF', 'C0603'): 1, ('100nF', 'C0603'): 1, ('1uF', 'C0603'): 1})

如果你想要更好的输出,请使用str.format和iterate:

with open('test.txt') as f:
    cn = Counter(tuple(islice(line.split(), 1, 3)) for line in f)
    for k, v in cn.items():
        print("{} {} {v}".format(*k, v=v))

输出:

10uF_Tantalum C0603 1
22uF C0603 1
1nF C0603 1
100nF C0603 1
1uF C0603 

答案 1 :(得分:-1)

如果我们已经以这种方式组织了数据:

elements = [
    ("a", "b"),
    ("c", "d"),
    ("a", "b"),
    ("c", "d"),
    ("a", "b"),
]

我们可以创建一组独特的元素:

elset = set(elements)

然后显示原始列表中每个唯一元素的出现次数:

for e in elset:
    print e[0], e[1], elements.count(e)