class Solution():
"""
s:待查数组
返回值没有重复的值列表
"""
#counts.get(word,0)+ 1 是指有word时返回其值,默认是0,+1能够累计次数;没有word时则返回0。
def theTwoNumbers(self, s):
counts = {}
for i in s:
counts[i] = counts.get(i,0) + 1
print(counts)
result = []
for key, value in counts.items():
if value == 1:
result.append(key)
return result
if __name__ == '__main__':
s = Solution()
arr = [1,2,5,1]
print("arr:",arr)
print("不重复的数:",s.theTwoNumbers(arr))
D:\ANACONDA\envs\pytorch_gpu\python.exe E:/Python面试准备/python基础/练习/300.py
arr: [1, 2, 5, 1]
{1: 2, 2: 1, 5: 1}
不重复的数: [2, 5]