# python列表获取索引 - python找索引

## 如何在Python中使用for循环在数组中查找重复元素? (10)

`````` list_a=[1,2,3,5,6,7,5,2]

tmp=[]

for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
``````

`````` for (int i=0;i<=list_a.length;i++)
for (int j=i+1;j<=list_a.length;j++)
if (list_a[i]==list_a[j])
print list_a[i]
``````

``````for i in list_a:
for j in list_a[1:]:
....
``````

``````from itertools import groupby
list_a = sorted([1, 2, 3, 5, 6, 7, 5, 2])
result = dict([(r, len(list(grp))) for r, grp in groupby(list_a)])
``````

``````{1: 1, 2: 2, 3: 1, 5: 2, 6: 1, 7: 1}
``````

``````import collections

x=[1, 2, 3, 5, 6, 7, 5, 2]

>>> x
[1, 2, 3, 5, 6, 7, 5, 2]

>>> y=collections.Counter(x)
>>> y
Counter({2: 2, 5: 2, 1: 1, 3: 1, 6: 1, 7: 1})
``````

``````>>> list(y)
[1, 2, 3, 5, 6, 7]
``````

``````>>> [i for i in y if y[i]>1]
[2, 5]
``````

``````>>> [i for i in y if y[i]==1]
[1, 3, 6, 7]
``````

C ++

``````for (int i=0;i<=list_a.length;i++)
for (int j=i+1;j<=list_a.length;j++)
if (list_a[i]==list_a[j])
print list_a[i]
``````

``````for i in range(0, len(list_a)):
for j in range(i + 1, len(list_a))
if list_a[i] == list_a[j]:
print list_a[i]
``````

c ++ for循环：

``````for(int x = start; x < end; ++x)
``````

Python等价物：

``````for x in range(start, end):
``````

``````dups = [x for x in list_a if list_a.count(x) > 1]
``````

``````import numpy as np
count,value = np.histogram(list_a,bins=np.hstack((np.unique(list_a),np.inf)))
print 'duplicate value(s) in list_a: ' + ', '.join([str(v) for v in value[count>1]])
``````

``````list_a=[1,2,3,5,6,7,5,2]
holding_list=[]

for x in list_a:
if x in holding_list:
pass
else:
holding_list.append(x)

print holding_list
``````

# 输出[1,2,3,5,6,7]

``````def removedup(List1,List2):
List1_copy = List1[:]
for i in List1_copy:
if i in List2:
List1.remove(i)

List1 = [4,5,6,7]
List2 = [6,7,8,9]
removedup(List1,List2)
print (List1)
``````

``````for i, elem in enumerate(seq):
if elem in seq[i+1:]:
print elem
``````

`````` pd.DataFrame(list_a, columns=["x"]).groupby('x').size().to_dict()
``````

`tmp = list(set(list_a))`