Help us understand the problem. What is going on with this article?

連想配列の特定のキーの値が同じものを一つに絞る

自分で調べていてあまり適切なものがネットで見つからなかったので、ここに残しておきます。
言語はPythonで書いておりますが、他の言語でも応用できるので、コメントを中心に読んでいってください。

# ピカチュウが被っているので
data = [
    {"species": "ピカチュウ", "color": "yellow" , "animal": "ねずみ"},
    {"species": "カイリュー", "color": "orange" , "animal": "ドラゴン"},
    {"species": "ヤドラン", "color": "pink" , "animal": "やどかり"},
    {"species": "ピカチュウ", "color": "orange" , "animal": "ねずみ"},
    {"species": "ビジョン", "color": "purple" , "animal": "とり"},
    {"species": "コダック", "color": "yellow" , "animal": "あひる"},
    {"species": "ピカチュウ", "color": "white" , "animal": "ねずみ"}
]

# こうして、ピカチュウを一つにしたい
new_data = [
    {"species": "ピカチュウ", "color": "yellow" , "animal": "ねずみ"},
    {"species": "カイリュー", "color": "orange" , "animal": "ドラゴン"},
    {"species": "ヤドラン", "color": "pink" , "animal": "やどかり"},
    {"species": "ビジョン", "color": "purple" , "animal": "とり"},
    {"species": "コダック", "color": "yellow" , "animal": "あひる"}
]

コード

data = [
    {"species": "ピカチュウ", "color": "yellow" , "animal": "ねずみ"},
    {"species": "カイリュー", "color": "orange" , "animal": "ドラゴン"},
    {"species": "ヤドラン", "color": "pink" , "animal": "やどかり"},
    {"species": "ピカチュウ", "color": "orange" , "animal": "ねずみ"},
    {"species": "ビジョン", "color": "purple" , "animal": "とり"},
    {"species": "コダック", "color": "yellow" , "animal": "あひる"},
    {"species": "ピカチュウ", "color": "white" , "animal": "ねずみ"}
]

# 重複を削りたいkeyの値を追加していく配列
species_array = []
# 新たにできる連想配列
new_data = []

# データ1つ1つに処理を行う
for pokemon in data:
    # 重複を削りたいkeyの値を変数に入れる
    species = pokemon["species"]
    # 重複を削りたいkeyの値を追加した配列に同じ値がなければ
    if(species not in species_array):
        # 重複を削りたいkeyの値を追加した配列に新たな値を追加して
        species_array.append(species)
        # 新たにできる連想配列にデータを追加する
        new_data.append(pokemon)

「他にもこんなやり方あるよ」とか、
「こういうコードの応用方程式集みたいなものあるよ」とか、
コメント残していってもらえると助かります。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした