2016-04-24 23 views
0

私はプログラミングが初めてで、私はリストから重複を削除しようとしています。しかし私はset()を使ってそれを実行することができません。リストには、IPアドレスが含まれており、日付、次は私のコードとリスト、私は次のエラーを取得するリストから重複する要素を削除する

l = [['10.136.161.80', '2015-08-29'], ['10.136.161.80', '2015-08-29'], ['10.136.161.80', '2015-08-29'], ['10.136.161.80', '2015-08-29'], ['10.136.161.80', '2015-08-29'], ['10.136.161.80', '2015-08-29'],['10.136.161.235', '2016-03-12'], ['10.136.161.235', '2015-05-02'], ['10.136.161.93', '2016-03-12'], ['10.136.161.93', '2016-03-12'], ['10.136.161.93', '2015-04-25'], ['10.136.161.93', '2015-04-25'], ['10.136.161.93', '2016-03-12'], ['10.136.161.93', '2015-04-25'], ['10.136.161.93', '2015-04-25'], ['10.136.161.93', '2015-11-28'], ['10.136.161.93', '2015-11-28'], ['10.136.161.80', '2015-08-29'], ['10.136.161.112', '2015-04-25'], ['10.136.161.231', '2015-04-25']] 

fl = set(l) 
print fl 

です:事前に

Traceback (most recent call last): 
    File "C:/Users/syeam02.TANT-A01/PycharmProjects/security/cleandata.py", line 18, in <module> 
    fl = set(array) 
TypeError: unhashable type: 'list' 

感謝。

答えて

3

listタイプの要素は、listが可変エンティティであるため、setには使用できません。同じ理由から、listを辞書のキーとして使用することはできません。 tupleのような不変型が必要です。

だから、あなたが設定して渡す前に、タプルに内側の要素を変換することができます:

set(tuple(li) for li in l) 

チェックthis section to doc

Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.

+0

感謝のRohitこれは私の問題を解決しました。 今私のデータはこのようになっています。異なる日付とIPは同じです。日付とIPを1つだけ保持することが可能です。 2015-08-29 10.136.161.80 2015-04-25 10.136.161.93 2015-04-25 2015年11月28日10.136.161.93 2016年4月2日10.136.161.231 2015年8月8日10.136.161.231 2015年11月28日10.136.161.235 2016年3月12日10.136.161.235 2015 10.136.161.231 -04-25 10.136.161.112 2015-05-02 10.136.161.235 2016-03-12 10.136.161.93 2015-11-28 10.136.161.231 2016-03-12 10.136.161.231 –

関連する問題