なぜこれが機能しないのですか?実際の結果は、任意のエントリに対して[]
です。コードが期待通りに実行されない[サイクル中]
def non_unique(ints):
"""
Return a list consisting of only the non-unique elements from the list lst.
You are given a non-empty list of integers (ints). You should return a
list consisting of only the non-unique elements in this list. To do so
you will need to remove all unique elements (elements which are
contained in a given list only once). When solving this task, do not
change the order of the list.
>>> non_unique([1, 2, 3, 1, 3])
[1, 3, 1, 3]
>>> non_unique([1, 2, 3, 4, 5])
[]
>>> non_unique([5, 5, 5, 5, 5])
[5, 5, 5, 5, 5]
>>> non_unique([10, 9, 10, 10, 9, 8])
[10, 9, 10, 10, 9]
"""
new_list = []
for x in ints:
for a in ints:
if ints.index(x) != ints.index(a):
if x == a:
new_list.append(a)
return new_list
(ない私から)ワーキングコード:x==a
が真であるかのよう
result = []
for c in ints:
if ints.count(c) > 1:
result.append(c)
return result
'ints.index(x)!= ints.index(a)'と 'x == a'という条件を同時に満たすことはできません。私はあなたが彼らがどのようになるかもしれないと想像していません。 – khelwood
どうしてなぜこんなことができないのか分かりません。最初はインデックス用、2番めの値用です。 –