简体   繁体   English

Python计数2变量(如果唯一)

[英]Python count 2 variable if unique

The code has to count the 2 variable if they are unique. 如果它们是唯一的,则代码必须计算2变量。
But i cant get it to work. 但是我不能让它工作。
Does someone have an idea how i can get it to work? 有人知道我如何使它工作吗?

I want the output to be: 我希望输出为:

C603 100nF 8

Here is a litle example of the .txt file. 这是.txt文件的一个小例子。
BaseBoard_V1.2_Componentlist.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

Code: 码:

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() )

Output: 输出:

[('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)]

I try'd to search on google, i try'd other code but it wont work for me. 我尝试在Google上搜索,我尝试其他代码,但对我来说不起作用。 Does someone have an idea how to fix this? 有人知道如何解决此问题吗?

If you want to count each element from the second and third elements as single elements just split, extract the second and third elements with itertools.islice and call tuple on the islice object passing it to Counter using a generator expression.: 如果要将第二个元素和第三个元素中的每个元素都视为单个元素进行拆分,请使用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))

Output from your example: 您的示例的输出:

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

If you want a nicer output use str.format and iterate: 如果您想要更好的输出,请使用str.format并进行迭代:

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))

Output: 输出:

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

Provided that we have data already organized in this way: 假设我们已经以这种方式组织了数据:

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

We can create a set of unique elements: 我们可以创建一组独特的元素:

elset = set(elements)

And then display how many times each unique element is present in the original list: 然后显示每个唯一元素出现在原始列表中的次数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM