Python读取并比较行和列

2024-10-01 13:30:44 发布

您现在位置:Python中文网/ 问答频道 /正文

0,1,foo
0,0,foo
0,1,foo
1,1,foobar
1,1,foobar
0,1,test
1,1,foobarbar

大约10000个条目。你知道吗

让它成为csv文件。你知道吗

我想知道第一列中有多少个“0”与Foo有关。以及第二列中与foo相关的“1”和“0”的数目。你知道吗

我是否阅读了文件中的前一行并进行了检查?有没有办法使用列表理解?我怎样在那里维护柜台?你知道吗

预期产量:

Foo
Coloumn1 : 
No. of 0's = 3
no. of 1's = 0
column 2:
no. of 1's =2
no. of 0's =1

Tags: 文件ofcsvnotest列表foo条目
3条回答

构建数据

datastring = """0,1,foo
0,0,foo
0,1,foo
1,1,foobar
1,1,foobar
0,1,test
1,1,foobarbar"""

def count_data(datastring):
    datadict = {}
    for line in datastring.split('\n'):
        col1, col2, col3 = line.split(',')
        for i, colval in enumerate((col1, col2)): # doing it this way in case there are more cols
            datadict.setdefault(col3, {}).setdefault(colval, [0, 0])[i] += 1
    return datadict
datadict = count_data(datastring)

输出:

{'test': {'1': [0, 1], '0': [1, 0]}, 'foobar': {'1': [2, 2]}, 'foo': {'1': [0, 2], '0': [3, 1]}, 'foobarbar': {'1': [1, 1]}}

显示数据

def print_data(datadict):
    for key in datadict:
        print key
        for i, col in enumerate(datadict[key]):
            print 'Column', i+1, ':'
            colvalues = datadict[key][col]
            for value in (0, 1):
                print "Number of {0}'s:".format(value), colvalues[value]

输出

test
Column 1 :
Number of 0's: 0
Number of 1's: 1
Column 2 :
Number of 0's: 1
Number of 1's: 0
foobar
Column 1 :
Number of 0's: 2
Number of 1's: 2
foo
Column 1 :
Number of 0's: 0
Number of 1's: 2
Column 2 :
Number of 0's: 3
Number of 1's: 1
foobarbar
Column 1 :
Number of 0's: 1
Number of 1's: 1
from collections import defaultdict, Counter
import csv

with open('myfile.csv', 'rb') as inf:
    incsv = csv.reader(inf)
    col1, col2 = defaultdict(Counter), defaultdict(Counter)
    for c1,c2,label in incsv:
        col1[label][c1] += 1
        col2[label][c2] += 1

labels = sorted(col1)
for lbl in labels:
    print('{}:'.format(lbl))
    print('Column1:')
    for entry in ['0', '1']:
        print("No. of {}'s = {}".format(entry, col1[lbl][entry]))
    print('Column2:')
    for entry in ['0', '1']:
        print("No. of {}'s = {}".format(entry, col2[lbl][entry]))

退货

foo:
Column1:
No. of 0's = 3
No. of 1's = 0
Column2:
No. of 0's = 1
No. of 1's = 2
foobar:
Column1:
No. of 0's = 0
No. of 1's = 2
Column2:
No. of 0's = 0
No. of 1's = 2
foobarbar:
Column1:
No. of 0's = 0
No. of 1's = 1
Column2:
No. of 0's = 0
No. of 1's = 1
test:
Column1:
No. of 0's = 1
No. of 1's = 0
Column2:
No. of 0's = 0
No. of 1's = 1
file = "a.csv"
search = "foo"
lines = open(file).readlines()
(firstcol_zero, firstcol_one, secondcol_zero, secondcol_one) = (0, 0 ,0 ,0 )
for line in lines:
    line = line.strip()
    if not line : continue
    split = line.split(',')
    if not split[2] == search: continue
    if (int(split[0]) == 0):    firstcol_zero += 1
    elif (int (split[0]) == 1): firstcol_one += 1
    if (int(split[1]) == 0):    secondcol_zero += 1
    elif (int (split[1]) == 1): secondcol_one += 1

print firstcol_zero
print firstcol_one
print secondcol_zero
print secondcol_one

相关问题 更多 >