<i id='DNLow'><tr id='DNLow'><dt id='DNLow'><q id='DNLow'><span id='DNLow'><b id='DNLow'><form id='DNLow'><ins id='DNLow'></ins><ul id='DNLow'></ul><sub id='DNLow'></sub></form><legend id='DNLow'></legend><bdo id='DNLow'><pre id='DNLow'><center id='DNLow'></center></pre></bdo></b><th id='DNLow'></th></span></q></dt></tr></i><div id='DNLow'><tfoot id='DNLow'></tfoot><dl id='DNLow'><fieldset id='DNLow'></fieldset></dl></div>
  • <tfoot id='DNLow'></tfoot>
  • <legend id='DNLow'><style id='DNLow'><dir id='DNLow'><q id='DNLow'></q></dir></style></legend>

      • <bdo id='DNLow'></bdo><ul id='DNLow'></ul>

      <small id='DNLow'></small><noframes id='DNLow'>

        如何计算列表中出现的唯一值

        How do I count occurrence of unique values inside a list(如何计算列表中出现的唯一值)
        <tfoot id='GfcFs'></tfoot>

          <small id='GfcFs'></small><noframes id='GfcFs'>

            1. <i id='GfcFs'><tr id='GfcFs'><dt id='GfcFs'><q id='GfcFs'><span id='GfcFs'><b id='GfcFs'><form id='GfcFs'><ins id='GfcFs'></ins><ul id='GfcFs'></ul><sub id='GfcFs'></sub></form><legend id='GfcFs'></legend><bdo id='GfcFs'><pre id='GfcFs'><center id='GfcFs'></center></pre></bdo></b><th id='GfcFs'></th></span></q></dt></tr></i><div id='GfcFs'><tfoot id='GfcFs'></tfoot><dl id='GfcFs'><fieldset id='GfcFs'></fieldset></dl></div>
                  <bdo id='GfcFs'></bdo><ul id='GfcFs'></ul>

                • <legend id='GfcFs'><style id='GfcFs'><dir id='GfcFs'><q id='GfcFs'></q></dir></style></legend>
                    <tbody id='GfcFs'></tbody>
                  本文介绍了如何计算列表中出现的唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我正在尝试编写此程序,该程序将要求用户输入并将值存储在数组/列表中。
                  然后,当输入空行时,它将告诉用户其中有多少值是唯一的。
                  我构建这个是出于现实生活的原因,而不是作为习题集。

                  enter: happy
                  enter: rofl
                  enter: happy
                  enter: mpg8
                  enter: Cpp
                  enter: Cpp
                  enter:
                  There are 4 unique words!
                  

                  我的代码如下:

                  # ask for input
                  ipta = raw_input("Word: ")
                  
                  # create list 
                  uniquewords = [] 
                  counter = 0
                  uniquewords.append(ipta)
                  
                  a = 0   # loop thingy
                  # while loop to ask for input and append in list
                  while ipta: 
                    ipta = raw_input("Word: ")
                    new_words.append(input1)
                    counter = counter + 1
                  
                  for p in uniquewords:
                  

                  ..到目前为止我得到的就是这些。
                  我不确定如何计算列表中唯一的字数?
                  如果有人能把解决方案张贴出来,让我从中学习,或者至少让我看看它有多棒,谢谢!

                  推荐答案

                  此外,使用collections.Counter重构您的代码:

                  from collections import Counter
                  
                  words = ['a', 'b', 'c', 'a']
                  
                  Counter(words).keys() # equals to list(set(words))
                  Counter(words).values() # counts the elements' frequency
                  

                  输出:

                  ['a', 'c', 'b']
                  [2, 1, 1]
                  

                  这篇关于如何计算列表中出现的唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                  <tfoot id='ik9AQ'></tfoot>

                        <tbody id='ik9AQ'></tbody>
                      • <small id='ik9AQ'></small><noframes id='ik9AQ'>

                          <i id='ik9AQ'><tr id='ik9AQ'><dt id='ik9AQ'><q id='ik9AQ'><span id='ik9AQ'><b id='ik9AQ'><form id='ik9AQ'><ins id='ik9AQ'></ins><ul id='ik9AQ'></ul><sub id='ik9AQ'></sub></form><legend id='ik9AQ'></legend><bdo id='ik9AQ'><pre id='ik9AQ'><center id='ik9AQ'></center></pre></bdo></b><th id='ik9AQ'></th></span></q></dt></tr></i><div id='ik9AQ'><tfoot id='ik9AQ'></tfoot><dl id='ik9AQ'><fieldset id='ik9AQ'></fieldset></dl></div>

                            <bdo id='ik9AQ'></bdo><ul id='ik9AQ'></ul>
                            <legend id='ik9AQ'><style id='ik9AQ'><dir id='ik9AQ'><q id='ik9AQ'></q></dir></style></legend>