使用python将每个列存储在单独的字典中 [英] Storing each column in a separate dictionary using python

查看:266
本文介绍了使用python将每个列存储在单独的字典中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



一个示例输入文件:(实际输入文件包含了一个有效的方法,可以使用python存储一个单独的字典中的制表符分隔文件的每一列?数百行和数百列。列数不是固定的,它会频繁更改。)

  ABC 
1 4 7
2 5 8
3 6 9

我需要打印值在mydict中的单元格中的 A

 
打印单元

并打印同一行中的值:

  for i in range(1,numrows):
for key in keysOfMydict:
print mydict [key] [i]


解决方案

最简单的方法是使用 DictReader csv 模块:

  with open('somefile.txt','r')as f:
reader = csv.DictReader(f,delimiter ='\t')
rows = list ader)#如果你的文件不大,你可以
#完全使用

#如果你的文件很大,你可能要
#读者中的行
#print(row ['A'])

行中的行
print(row ['A'])

@Marius提出了一个好点 - 您可能希望通过标题分开收集所有列。 / p>

如果是这样,你必须调整阅读逻辑:

 code>从集合导入defaultdict 
by_column = defaultdict(list)

行中的行:
for k,v in row.iteritems():
by_column [k] .append(v)

另一个选项是 pandas

 >>>将大熊猫导入为pd 
>>> i = pd.read_csv('foo.csv',sep ='')
>>> i
A B C
0 1 4 7
1 2 5 8
2 3 6 9
>>>我['A']
0 1
1 2
2 3
名称:A,dtype:int64


Is there an efficient way to store each column of a tab-delimited file in a separate dictionary using python?

A sample input file: (Real input file contains thousands of lines and hundreds of columns. Number of columns is not fixed, it changes frequently.)

A B C
1 4 7
2 5 8
3 6 9

I need to print values in column A:

for cell in mydict["A"]:
    print cell

and to print values in the same row:

for i in range(1, numrows):
    for key in keysOfMydict:
        print mydict[key][i]

解决方案

The simplest way is to use DictReader from the csv module:

with open('somefile.txt', 'r') as f:
   reader = csv.DictReader(f, delimiter='\t')
   rows = list(reader) # If your file is not large, you can
                       # consume it entirely

   # If your file is large, you might want to 
   # step over each row:
   #for row in reader:
   #    print(row['A'])

for row in rows:
   print(row['A'])

@Marius made a good point - that you might be looking to collect all columns separately by their header.

If that's the case, you'll have to adjust your reading logic a bit:

from collections import defaultdict
by_column = defaultdict(list)

for row in rows:
   for k,v in row.iteritems():
       by_column[k].append(v)

Another option is pandas:

>>> import pandas as pd
>>> i = pd.read_csv('foo.csv', sep=' ')
>>> i
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9
>>> i['A']
0    1
1    2
2    3
Name: A, dtype: int64

这篇关于使用python将每个列存储在单独的字典中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆