【问题标题】:How do i check the 2 item of every string for more than one occurence我如何检查每个字符串的 2 项是否不止一次出现
【发布时间】:2020-09-09 09:31:09
【问题描述】:
[‘AC’, ‘2H’, ‘3S’, ‘4C’]

如何检查每个字符串的第一个索引(例如第二个元素)是否多次出现?例如,在这种情况下,C 出现了 2 次,所以我需要返回 False 这也必须适用于其他情况,例如多次出现的 H 或 S

【问题讨论】:

    标签: python string list iteration


    【解决方案1】:

    考虑使用collections.Counter 来计算感兴趣项目的出现次数。并使用allany 来验证条件。

    import collections
    
    a = ['AC', '2H', '3S', '4C']
    counter = collections.Counter(s[1] for s in a)
    result = all(v < 2 for v in counter.values())
    
    print(result)
    

    【讨论】:

      【解决方案2】:

      你可以使用这个功能:

      def check_amount(all_text, character):
          count = 0
          for text in all_text:
              for ch in text:
                  if ch == character:
                      count += 1
          return count
      

      这会返回它发生的次数,如果你只是想看看它是否存在:

      def check_amount(all_text, character):
          for text in all_text:
              for ch in text:
                  if ch == character:
                      return True
                  else:
                      return False
      

      这些用于在任何位置进行检查,如果您需要它位于您所说的特定位置:

      def check_amount(all_text, character):
          count = 0
          for text in all_text:
              if text[1] == character:
                  count += 1
          return count
      

      如果您想要布尔版本,则可以使用不使用计数的相同方法更改此设置

      all_text 是您要传入的列表,character 是您要查看是否存在/存在的列表。

      【讨论】:

        【解决方案3】:

        使用正则表达式,您可以使用re.finditer 查找所有(非重叠)出现:

        >>> import re
        >>> text = 'Allowed Hello Hollow'
        >>> for m in re.finditer('ll', text):
                 print('ll found', m.start(), m.end())
        
        ll found 1 3
        ll found 10 12
        ll found 16 18
        

        或者,如果你不想要正则表达式的开销,你也可以重复使用 str.find 来获取下一个索引:

        >>> text = 'Allowed Hello Hollow'
        >>> index = 0
        >>> while index < len(text):
                index = text.find('ll', index)
                if index == -1:
                    break
                print('ll found at', index)
                index += 2 # +2 because len('ll') == 2
        
        ll found at  1
        ll found at  10
        ll found at  16
        This also works for lists and other sequences.
        

        对于这里的数组,我会使用 List Comprehension,如下所示:

        listOfElems = ['Hello', 'Ok', 'is', 'Ok', 'test', 'this', 'is', 'a', 'test', 'Ok']
        

        现在让我们在列表中找到所有 'ok' 的索引

        # Use List Comprehension Get indexes of all occurrences of 'Ok' in the list
        indexPosList = [ i for i in range(len(listOfElems)) if listOfElems[i] == 'Ok' ]
        
        print('Indexes of all occurrences of "Ok" in the list are: ', indexPosList)
        

        输出:

        Indexes of all occurrences of "Ok" in the list are :  [1, 3, 9]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-22
          • 1970-01-01
          • 2022-12-14
          • 2010-10-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多