简体   繁体   English

我在弄清楚这个压缩代码时遇到麻烦

[英]I'm having trouble figuring out this condensed code

def duplicate_count(s):
    return len([c for c in set(s.lower()) if s.lower().count(c)>1])

I'm having difficulty understanding how this code works. 我很难理解此代码的工作方式。 I was doing a codewars challenge to return the number of elements with duplicates in a string. 我正在进行代码战挑战,以返回字符串中重复的元素数量。

eg. 例如。 Asasd --> 2 Asasd-> 2

I came up with my own implmentation but I wasn't able to really understand what this code does. 我想出了自己的实现,但我无法真正理解此代码的作用。 If anyone could point me in the direction, it would be much appreciated :) 如果有人能指出我的方向,将不胜感激:)

This is, first of all, a highly inefficient solution to the problem. 首先,这是解决该问题的效率极低的解决方案。 But let's break it down: 但让我们分解一下:

  • s.lower() would convert all the characters in a string to lower case: s.lower()会将字符串中的所有字符转换为小写:

     In [1]: s = "Hello, WORLD" In [2]: s.lower() Out[2]: 'hello, world' 
  • set(s.lower()) would create a set (make sure to read about what sets are) of characters from a string - eliminating all the duplicates: set(s.lower())将从字符串中创建一个字符 (确保了解什么是字符集)-消除所有重复项:

     In [3]: set(s.lower()) Out[3]: {' ', ',', 'd', 'e', 'h', 'l', 'o', 'r', 'w'} 
  • for c in set(s.lower()) iterates over every single character in a set we created above for c in set(s.lower())我们遍历上面创建的集合中的每个单个字符
  • for every character in this set, we apply this if condition: if s.lower().count(c)>1 . 对于此集合中的每个字符,我们将在if条件下应用此条件: if s.lower().count(c)>1 The count(c) here would count how many times c appears in the string. 这里的count(c)将计算c在字符串中出现的次数。 The >1 helps us to leave characters that are met more than 1 time in a string >1可帮助我们在字符串中保留多次遇到的字符
  • [c for c in set(s.lower()) if s.lower().count(c)>1] is called a list comprehension . [c for c in set(s.lower()) if s.lower().count(c)>1] 称为列表推导 It is basically a short way of creating a list. 基本上,这是创建列表的一种简短方法。 Here, we are creating a list of characters that occur in a string more than one time. 在这里,我们创建了一个字符列表,这些字符在一个字符串中出现多次。 Check out this topic about how to verbalize and read the list comprehensions . 查看有关如何表达和阅读列表理解的主题
  • len() then just gets us the length of the list len()然后获取列表的长度

To summarize, you iterate over the unique characters in a given string and count which of them occur in a string more than one time. 总而言之,您遍历给定字符串中的唯一字符,并计算其中哪些字符多次出现在字符串中。

set(s.lower()) # gives unique elements in lower case

and

s.lower().count(c)>1 #checks if an element shows up more than once

All in all the function finds number of not unique elements in a string ,ignoring case. 总之,该函数查找字符串中不唯一的元素的数量,而忽略大小写。

I believe using collections.Counter is more efficient: 我相信使用collections.Counter会更高效:

In [7]: from collections import Counter

In [8]: sum(v > 1 for v in Counter("Aabcc".lower()).values())

Out[8]: 2
def duplicate_count(s):

    result = []

    for c in set(s.lower()):
        if s.lower().count(c) > 1:
            result.append(c)

    return len(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM