计算长字符串中的字母(仅字母)(python 2.72)

发布于 2024-12-18 21:01:40 字数 403 浏览 1 评论 0原文

我需要编写一个接收长字符串并放入字典的函数 每个字母,以及它在字符串中的出现频率。 我写了下一个函数,但问题是它没有忽略空格、数字等。 我被要求使用函数 symbol in string.ascii_lowercase,但我不知道该怎么做。 这是我的代码:

def calc_freq(txt):
   dic={}
   for letter in range(len(txt)):
           if dic.has_key(txt[letter])==True:
                dic[txt[letter]] += 1
           else:
            dic[txt[letter]] = 1
   return dic

感谢您的帮助。

i need to write a function which receives a long string, and puts into a dictionary
each letter, and it's it's appearance frequency in the string.
iv'e written the next function, but the problem it doesn't ignore whitespaces, numbers etc..
iv'e been asked to use the function symbol in string.ascii_lowercase, but iv'e no idea how to do it.
this is my code:

def calc_freq(txt):
   dic={}
   for letter in range(len(txt)):
           if dic.has_key(txt[letter])==True:
                dic[txt[letter]] += 1
           else:
            dic[txt[letter]] = 1
   return dic

thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

银河中√捞星星 2024-12-25 21:01:40

只是为了好玩:

s = 'Type "help", "copyright", "credits" or "license" for more information.'
print dict(filter(lambda i: str.isalnum(i[0]), set(zip(a,map(a.count,a)))))

{'a': 1, 'c': 3, 'e': 6, 'd': 1, 'g': 1, 'f': 2, 'i': 5, 'h': 2、‘米’:
2、“l”:2、“o”:6、“n”:3、“p”:3、“s”:2、“r”:6、“t”:3、“y”:2、
“T”:1}

just for fun:

s = 'Type "help", "copyright", "credits" or "license" for more information.'
print dict(filter(lambda i: str.isalnum(i[0]), set(zip(a,map(a.count,a)))))

{'a': 1, 'c': 3, 'e': 6, 'd': 1, 'g': 1, 'f': 2, 'i': 5, 'h': 2, 'm':
2, 'l': 2, 'o': 6, 'n': 3, 'p': 3, 's': 2, 'r': 6, 't': 3, 'y': 2,
'T': 1}

挽清梦 2024-12-25 21:01:40
import string

s = 'oasndoasndoansdakls'
count = []
dictionary = {}

for x in set(s):
     if x in string.ascii_lowercase:
          dictionary[x] = s.count(x)

print (dictionary)

这将创建一个字符及其计数的字典,并且仅当它们位于 string.ascii_lowercase 列表中时才包含它们。


以下是如何在代码中使用它:

import string

def calc_freq(txt):
     dic={}
     for letter in txt:
          if letter in string.ascii_lowercase:
               if letter in dic:
                    dic[letter] += 1
               else:
                    dic[letter] = 1
     return dic

您只需在将字母添加到字典或增加其计数之前添加一个 if 语句。

我还删除了 letter in range(txt)txt[letter],您可以直接在 python 中访问每个字符,因为字符串是一个 iterable code> 并且可以像列表一样对待。

import string

s = 'oasndoasndoansdakls'
count = []
dictionary = {}

for x in set(s):
     if x in string.ascii_lowercase:
          dictionary[x] = s.count(x)

print (dictionary)

this will create a dictionary of the charactors and their counts, and only include them if they are in the string.ascii_lowercase list.


Here is how to use it in your code:

import string

def calc_freq(txt):
     dic={}
     for letter in txt:
          if letter in string.ascii_lowercase:
               if letter in dic:
                    dic[letter] += 1
               else:
                    dic[letter] = 1
     return dic

you just needed to add an if statment before you add the letter to the dictionary or increase its count.

I also removed the letter in range(txt) and txt[letter], you can access each charactor directly in python, because a string is an iterable and can be treated similar to a list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文