dongxia8656 2013-12-23 13:35
浏览 23
已采纳

在单个数组中查找重复的键

I have a dictionary array like below

$foo = array(
    'key1' => 'value 1',
    'key1' => 'value 2',
    'key2' => 'value 3',
    'key2' => 'value 4',
    'key3' => 'value 5'
);

as you can see there are duplicate keys. All keys are strings. Array sits in a file and was created manually. It has close to a 1000 entries with potentially many duplicate keys.

How can I find out which keys are duplicates?

The result I want to get is a list of the keys that have duplicates:

key1, key2, ...

so I can go in the file and fix those keys to make them unique. Any format is fine, just so I know the names of those keys.

key3 has no duplicate so it's fine.

Any help would be greatly appreciated.

Thanks.

Most of you seem to be telling me the same thing :) I know I can't have multiple keys. The problem is that this is a dictionary array created manually in a flat file. The person who created it added the same keys multiple times. The problem is that key1 on one page has translation A but on other page it should have translation B, but since both values have the same key in my dictionary array they both display the same value.

  • 写回答

6条回答 默认 最新

  • duansha7453 2013-12-23 13:41
    关注

    I assume this is a one-time job in order to clean out some input data in a file and not something you that needs to happen automatically.

    If your data originally is in a CSV type file and you have access to some GNU tools, I often use something like

    $ cat filenamv.csv | cut -d, -f1  | sort | uniq -d
    

    This should the first column of the CSV file and print any duplicate keys.

    You probalby want to read up on the individual commands (example man uniq) for just the correct parameters to use in your case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序