LoginSignup
3
4

More than 5 years have passed since last update.

リストの中の重複した要素を一つにして新しいリストを作る

Posted at

["a","a","a",2,3,3,2,2]を['a',2,3]とする方法。

def remove_duplicates(x):
    list_a = []
    for index,i in enumerate(x):
        list_a.append(i)
        if list_a.count(i) > 1:
            list_a.remove(i)
    return list_a
print remove_duplicates(["a","a","a",2,3,3,2,2])
#['a',2,3]

というか、以下でよかったみたい。

def remove_duplicates(x):
    y=[]
    for i in x:
        if i not in y:
            y.append(i)
    return y
3
4
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4