Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计数一次出现的列表条目数_Python_Arrays_List_Loops_Unique - Fatal编程技术网

Python 计数一次出现的列表条目数

Python 计数一次出现的列表条目数,python,arrays,list,loops,unique,Python,Arrays,List,Loops,Unique,我正在尝试编写一个Python函数,用于计算列表中恰好出现一次的条目数 例如,给定列表[17],此函数将返回1。或者给定[3,3,-22,1,-22,1,3,0],它将返回1 **限制:我不能将任何内容导入我的程序 到目前为止,我所写的错误代码是:我要走双循环路线,但是索引数学变得过于复杂 def count_unique(x): if len(x) == 1: return 1 i = 0 j = 1 for i in range(len(x)): for j

我正在尝试编写一个Python函数,用于计算列表中恰好出现一次的条目数

例如,给定列表[17],此函数将返回1。或者给定[3,3,-22,1,-22,1,3,0],它将返回1

**限制:我不能将任何内容导入我的程序

到目前为止,我所写的错误代码是:我要走双循环路线,但是索引数学变得过于复杂

def count_unique(x):
  if len(x) == 1:
    return 1
  i = 0
  j = 1
  for i in range(len(x)):
    for j in range(j,len(x)):
      if x[i] == x[j]:
        del x[j]
        j+1
    j = 0
  return len(x)
对不起:)

您的解决方案不起作用,因为您正在做一些奇怪的事情。在遍历列表时删除列表中的内容,j+1没有意义等。尝试添加新列表中唯一的元素,然后计算其中的内容数。然后找出我的解决方案的作用

以下是O(n)解决方案顺便说一句:

lst =  [3,3,-22,1,-22,1,3,0,37]
cnts = {}
for n in lst:
   if n in cnts:
      cnts[n] = cnts[n] + 1
   else:
      cnts[n] = 1

count = 0
for k, v in cnts.iteritems():
   if v == 1:
      count += 1
print count

由于您不能使用
集合.Counter
排序的
/
itertools.groupby
(其中之一通常是我的go-to解决方案,取决于输入是可散列的还是可排序的),因此只需模拟与
计数器大致相同的行为即可,计算所有元素,然后计算最后只出现一次的元素数:

def count_unique(x):
    if len(x) <= 1:
        return len(x)
    counts = {}
    for val in x:
        counts[val] = counts.get(val, 0) + 1
    return sum(1 for count in counts.values() if count == 1)
def count_unique(x):

如果len(x)是一个更简单易懂的解决方案:

l =  [3, 3, -22, 1, -22, 1, 3, 0]
counter = 0

for el in l:
    if l.count(el) == 1:
        counter += 1

这很简单。您可以迭代列表中的项目。然后查看元素在列表中是否正好有一次,然后添加+1。您可以改进代码(使列表理解、使用lambda表达式等),但这是所有代码背后的想法,也是最容易理解的,我认为。

您将这变得过于复杂。尝试使用字典,其中键是列表中的元素。这样,如果它存在,它将是独一无二的

再加上。当考虑复杂性时,这可能是最好的方法。在
字典中查找的
被认为是
O(1)
,for循环是
O(n)
,因此总的时间复杂度是
O(n)
,这是可取的。。。对列表元素使用count()在整个列表中搜索基本上是
O(n^2)
。。。那太糟糕了

from collections import defaultdict
count_hash_table = defaultdict(int) # i am making a regular dictionary but its data type is an integer
elements = [3,3,-22,1,-22,1,3,0]
for element in elements:
    count_hash_table[element] += 1 # here i am using that default datatype to count + 1 for each type

print sum(c for c in count_hash_table.values() if c == 1): 

你能出示你的密码吗?到目前为止,您尝试了什么?@idjaw请参见编辑。这是非常错误的,但这是我现在得到的也许你应该迭代集合(l)而不是l。你能做得更好吗?使用count是一种欺骗(我不知道它的存在,我对python只懂一半),这是一篇关于代码的有趣帖子:)尽管与其他解决方案不同,它确实意味着
O(n^2)
work;显式迭代列表一次,隐式迭代列表中的每一项。对于大型列表,这将是非常昂贵的。基于
dict
的解决方案在
O(n)
时间内运行。是的,它不是最快的解决方案。我从来没有这样说过。我只是说这很容易理解。我觉得OP需要它,而不是为此做一些高级算法。但我很高兴在这里投票选出所有好的答案,这也考虑到了成本!有几个人需要投票。一些很好的解决方案,也是一个双过滤器:)我在这里看不到比O(n^2)更好的解决方案。@MK。我下面的答案是2O(n),它比O(n^2)好得多
l =  [3, 3, -22, 1, -22, 1, 3, 0]
counter = 0

for el in l:
    if l.count(el) == 1:
        counter += 1
from collections import defaultdict
count_hash_table = defaultdict(int) # i am making a regular dictionary but its data type is an integer
elements = [3,3,-22,1,-22,1,3,0]
for element in elements:
    count_hash_table[element] += 1 # here i am using that default datatype to count + 1 for each type

print sum(c for c in count_hash_table.values() if c == 1):