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 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答