欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python Leetcode 字符串中的第一个唯一字符

程序员文章站 2022-07-24 12:06:18
利用到了python中字典的collections.Counter()函数 collections中函数Counter的使用和用法: counter工具用于支持便捷和快速地计数, from collections import Counter cnt = Counter() for word in ......

利用到了python中字典的collections.counter()函数

collections中函数counter的使用和用法:

  counter工具用于支持便捷和快速地计数,

  from collections import counter

  cnt = counter()

  for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:

    cnt[word] += 1

  print cnt

  输出为

  counter({'blue': 3, 'red': 2, 'green': 1})

  快速实现了题目中所要求的只出现一次