从字典列表中删除重复的键
我是 Python 新手,目前被这个问题难住了:
我有一个生成的字典列表 csv.DictReader
。我使用以下函数创建了列表:
def csvToDictList(filename):
reader = csv.DictReader(open(filename, 'rb'))
list = []
for row in reader:
list.append(row)
return (list, reader.fieldnames)
这很好用,但是我正在处理的 CSV 文件有重复的列,所以我最终得到一个字典,如下所示:
[
{'Column1': 'Value1', 'Column2': 'Value2', ... <some unique columns and values> ..., 'Column1': 'Value1', 'Column2': 'Value2'},
...
{'Column1': 'Value1N', 'Column2': 'Value2N', ... <some unique columns and values> ..., 'Column1': 'Value1N', 'Column2': 'Value2N'}
]
我的主要问题是如何从中删除重复的列字典列表?
我考虑过迭代每个键,然后在检测到重复的键名称时删除该列,如下所示:
def removeColumn(dictList, colName):
for row in dictList:
del row[colName]
但是,这不会删除两列吗?我应该对字典的哈希键进行操作吗?任何帮助表示赞赏!
编辑:我看到的重复项实际上存在于reader.fieldnames
列表中。因此,我假设字典也包含这些列,这是一个错误的假设。
I am new to Python, and I am currently stumped by this problem:
I have a list of dictionaries generated csv.DictReader
. I have created the list with the function as follows:
def csvToDictList(filename):
reader = csv.DictReader(open(filename, 'rb'))
list = []
for row in reader:
list.append(row)
return (list, reader.fieldnames)
This worked great, but CSV file I am processing has duplicate columns, so I end up with a dictionary like:
[
{'Column1': 'Value1', 'Column2': 'Value2', ... <some unique columns and values> ..., 'Column1': 'Value1', 'Column2': 'Value2'},
...
{'Column1': 'Value1N', 'Column2': 'Value2N', ... <some unique columns and values> ..., 'Column1': 'Value1N', 'Column2': 'Value2N'}
]
My main question is how do I remove duplicate columns out of this dictionary list?
I thought about iterating over each key, and then removing the column when I detect a duplicate key name with something like this:
def removeColumn(dictList, colName):
for row in dictList:
del row[colName]
But, won't that remove both columns? Should I be operating on the hash-keys of the dictionary? Any help is appreciated!
EDIT : The duplicates I was seeing were actually present in the reader.fieldnames
list. So, I was assuming the dictionaries contained these columns as well, which was an incorrect assumption.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字典中没有什么比重复键更重要的了。
如果您有多个同名列,DictReader 将仅获取最后一列(覆盖之前的列)。
对于以下 CSV 文件:
DictReader 将返回以下字典:
从而丢弃
a
和b
列的先前值。There is nothing like duplicate keys in a dictionary.
If you have more columns with the same name, DictReader will take only the last one (overwriting the previous ones).
For the following CSV file:
the DictReader will return following dicts:
thus throwing the previous values for
a
andb
columns away.