从字典列表中删除重复的键

发布于 2024-12-11 17:04:42 字数 1008 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2024-12-18 17:04:42

字典中没有什么比重复键更重要的了。

如果您有多个同名列,DictReader 将仅获取最后一列(覆盖之前的列)。

对于以下 CSV 文件:

a,b,c,a,b
1,2,3,4,5
6,7,8,9,10

DictReader 将返回以下字典:

{'a': '4', 'c': '3', 'b': '5'}
{'a': '9', 'c': '8', 'b': '10'}

从而丢弃 ab 列的先前值。

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:

a,b,c,a,b
1,2,3,4,5
6,7,8,9,10

the DictReader will return following dicts:

{'a': '4', 'c': '3', 'b': '5'}
{'a': '9', 'c': '8', 'b': '10'}

thus throwing the previous values for a and b columns away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文