Python 基于字典出现次数的计数';s键在.CSV文件中
我有一个口述,看起来像这样:Python 基于字典出现次数的计数';s键在.CSV文件中,python,string,csv,dictionary,Python,String,Csv,Dictionary,我有一个口述,看起来像这样: {100002: "['Apple', 'M', 'R', '500001', 'Fruit', '90']", 100004: "['Banana', 'M', 'Y', '500001', 'Fruit', '75']"} 100001,1 100001,1 100001,2 100002,1 100002,1 100002,3 100002,3 100003,1 100003,4 100004,2 100004,3 1
{100002: "['Apple', 'M', 'R', '500001', 'Fruit', '90']", 100004: "['Banana', 'M', 'Y', '500001', 'Fruit', '75']"}
100001,1
100001,1
100001,2
100002,1
100002,1
100002,3
100002,3
100003,1
100003,4
100004,2
100004,3
100004,3
键是整数,值是字符串
我有一个.csv文件,如下所示:
{100002: "['Apple', 'M', 'R', '500001', 'Fruit', '90']", 100004: "['Banana', 'M', 'Y', '500001', 'Fruit', '75']"}
100001,1
100001,1
100001,2
100002,1
100002,1
100002,3
100002,3
100003,1
100003,4
100004,2
100004,3
100004,3
我想计算给定键在第二列中每个数字出现的次数,并将该次数添加到我的dict中。因此,对于此示例,100001的1为2,2为1,100002的1为2,3为2,100003的1为1,4为1,100004的计数为1表示2,2表示3。虽然此.csv文件包含大量键的数据(其中dict中的键是其中的一个子集),但我希望将这些计数附加到dict中,使其看起来像这样(每个键添加4个新值,每个值依次对应数字1-4的计数)
这4个添加的部分是数字1-4的计数,因此100002具有'2','0','2','0'
,因为在.csv文件中有2行100002,1
但0行100002,2
和2行100002,3
但0行100002,4
我的问题有两部分。1) 在.csv文件中,当一个键后跟一个1-4之间的数字时,我如何计算次数,以便我有4次计数(数字1-4各一次)?2) 如何将这些计数添加到字典中
答复:
根据公认的答案,我精心设计了这个。这比我想象的要难看一点,但我还是设法把它完成了
dd = defaultdict(lambda: defaultdict(int))
with open('AgentsCorpLevel.csv') as fin:
csvin = csv.reader(fin)
for row in csvin:
if int(row[0]) in MyDict.keys():
dd[int(row[0])][row[1]] += 1
print dd
dicts = MyDict,dd
#print dicts
FullDict = {}
PartlyCleanedDict = {}
CleanedDict = {}
TwoTypeDict = {k:[d.get(k) for d in dicts] for k in {k for d in dicts for k in d}}
for key, value in TwoTypeDict.iteritems():
FullDict.setdefault((int(key)), str(value))
for key, value in FullDict.iteritems():
PartlyCleanedDict.setdefault((int(key)), value.translate(None, "[]{()\/\'\"<>").replace('}',',}'))
for key, value in PartlyCleanedDict.iteritems():
CleanedDict.setdefault((int(key)), value.replace(',defaultdicttype int', ''))
print CleanedDict
不幸的是,我试图完全“清理”产生的CleanedICT没有起作用,因为这里有一个CleanedICT的打印
的示例(注意,我在这里只给出了3个键,我已经更改了名称,以适应我的样本中的水果和蔬菜主题
{1000132: 'Kiwi, S, B, 500006, Fruit, 3n, defaultdicttype int, 1: 2, 3: 4, 2: 4, 4: 1,}', 1000103: 'Iceberg Lettuce, M, G, 500004, Vegetable, 2n, defaultdicttype int, 1: 3, 3: 3, 2: 3, 4: 3,}',1000137: 'Pineapple, M, Y, 500006, Fruit, 45n,defaultdicttype int, 1: 5, 3: 4, 2: 7, 4: 1,}'}
您可以使用嵌套的
defaultdict
-我将把4个以上值的微调和处理以及精确的格式设置等留给您
import csv
from collections import defaultdict
d = {100002: "['Apple', 'M', 'R', '500001', 'Fruit', '90']", 100004: "['Banana', 'M', 'Y', '500001', 'Fruit', '75']"}
dd = defaultdict(lambda: defaultdict(int))
with open('test.csv') as fin:
csvin = csv.reader(fin)
for row in csvin:
dd[int(row[0])][row[1]] += 1
for key in (key for key in dd if key in d):
counts = [0] * 4
for idx, val in dd[key].iteritems():
counts[int(idx) - 1] = int(val)
print key, d[key], counts
# 100002 ['Apple', 'M', 'R', '500001', 'Fruit', '90'] [2, 0, 2, 0]
# 100004 ['Banana', 'M', 'Y', '500001', 'Fruit', '75'] [0, 1, 2, 0]
您可以使用嵌套的
defaultdict
-我将把4个以上值的微调和处理以及精确的格式设置等留给您
import csv
from collections import defaultdict
d = {100002: "['Apple', 'M', 'R', '500001', 'Fruit', '90']", 100004: "['Banana', 'M', 'Y', '500001', 'Fruit', '75']"}
dd = defaultdict(lambda: defaultdict(int))
with open('test.csv') as fin:
csvin = csv.reader(fin)
for row in csvin:
dd[int(row[0])][row[1]] += 1
for key in (key for key in dd if key in d):
counts = [0] * 4
for idx, val in dd[key].iteritems():
counts[int(idx) - 1] = int(val)
print key, d[key], counts
# 100002 ['Apple', 'M', 'R', '500001', 'Fruit', '90'] [2, 0, 2, 0]
# 100004 ['Banana', 'M', 'Y', '500001', 'Fruit', '75'] [0, 1, 2, 0]
我可以建议你对数据进行更多的结构化吗?有数字键和求和已经够令人困惑的了——我建议使用一个dict的dict,内部dict将计数存储为键,频率存储为值。我不反对一些额外的数据结构化,但我不知道如何执行你的建议。这个dict会如何计数数:频率使用我已经拥有的数据作为我的主字典的值,而不覆盖它?我可以建议你对数据进行更多的结构化吗?有数字键和数字和已经足够混乱了-我建议有一个dict的dict,内部dict将计数数存储为键,频率存储为值。我不反对一些额外的数据结构,但我不知道如何执行您的建议。计数编号:频率这一指令如何在不覆盖它的情况下使用我已经作为主词典值的数据?一个可靠的开始:)这会产生一个错误:计数[int(idx)-1]=int(val)索引器:列表分配索引超出范围我得到了一些基于此的结果,并且您的答案是唯一的,因此您得到了“接受”:)一个可靠的开始:)这会产生一个错误:counts[int(idx)-1]=int(val)索引器:列表分配索引超出范围我从中得到了一些有用的东西,而你的答案是唯一的,所以你得到了“接受”: