小编add*_*tes的帖子

如何在python 3中创建所有字母(az)的txt频率计数器

我有一个名为textf的文本文件,如下所示:

rxgmgcwbd c qcyurr bkxgmq, lwrg grru rrwxtam rwgzwt am quyam cv avrrgdwkxgcr.iwxbdamcz xdalguj qarc ram av vcmfwgmgum. yw'g
Run Code Online (Sandbox Code Playgroud)

我想对文本文件中的每个字母进行频率计数,但我希望它的条件是如果文本中没有出现字母,它应该有一个键值:值为0.例如,如果z不是在文本中它应该看起来像'z':0等等所有字母(a到z).我做了以下代码:

import string  
from collections import Counter 
with open("textf.txt") as tf: 
    letter = tf.read()
letter_count = Counter(letter.translate(str.maketrans('','',string.punctuation)))
print("Frequency count of letter:","\n",letter_count)
Run Code Online (Sandbox Code Playgroud)

但输出看起来像这样:

Counter({' ': 110, 'r': 12, 'c': 88, 'a': 55, 'g': 57, 'w': 76, 'm': 76, 'x': 72, 'u': 70, 'q': 41, 'y': 40, 'j': 36, 'l': 32, 'b': 18, 'd': 28, 'v': 27, 'k': 22, 't': 19, 'f': 18, …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

8
推荐指数
3
解决办法
1293
查看次数

类型错误:调用函数时必须是 str,而不是在 Python 3 中列出

我创建了一个函数来计算一个字母,例如字母 e。我的函数看起来与此类似:

def count_letter(sentence, accents, case):

    lower_case_e = ['e']
    upper_case_E = ['E']
    accent_lower_case = ['é', 'ê', 'è']
    accent_upper_case = ['É', 'Ê', 'È']

    for character in sentence:#If statement for optional argument where ignore_accents == True and ignore_case == False.
        #This loop will count lower case and upper case e as differente letters but will treat accented characters the same.

        if accents == True and case == False:
            lower_case_count = sentence.count(lower_case_e)
            accent_lower_case_count = sentence.count(accent_lower_case)
            upper_case_count = sentence.count(upper_case_E)
            accent_upper_case_count = sentence.count(accent_upper_case)

            total_e_count = …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

6
推荐指数
1
解决办法
1万
查看次数

在python 3中水平显示列表结果而不是垂直显示

我在python中创建了一个函数来计算单词的长度,而忽略标点符号。例如,如果我有一个句子:"Haven't there been 3 blackouts today?结果应该是以下内容:[6,5,4,1,9,5]我可以使用我创建的以下函数获得这些结果:

import string
def word_length_list(given_string):
        clean_string = given_string.translate(str.maketrans('','',string.punctuation))
        word_lenght_list = list(map(len, clean_string.split()))
        return word_lenght_list
    given_string = "Haven't there been 3 blackouts today?"
    word_length_list(given_string)
Run Code Online (Sandbox Code Playgroud)

此函数给我的预期结果是:[6, 5, 4, 1, 9, 5] 但是,如果给它一个很长的字符串,例如:

given_string = "Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output …
Run Code Online (Sandbox Code Playgroud)

python python-3.x jupyter-notebook

5
推荐指数
1
解决办法
1445
查看次数

如何在python中比较两个列表列表

我有两个列表列表,如下所示:

List_1 = [[10.7,7.2],[6.3,5.0],[8.9,10.0]]
List_2 = [[10.7,6.8],[6.3,4.9],[8.9,12.7]]
Run Code Online (Sandbox Code Playgroud)

我想创建一个名为List_3的第三个列表,它只包含List_2对,其中List_2中对的第二个值大于List_1中对的第二个值.例如,在这种情况下,List_3将是一个如下所示的列表:

List_3 = [[8.9,12.7]]
Run Code Online (Sandbox Code Playgroud)

因为第二个值12.7是唯一一个大于List_1中对的第二个值的值.换句话说,我想将List_1中的所有列表与List_2中的所有列表进行比较,并且仅获取List_2中的列表,其中List_1的n大于List_2的n,其中列表1和2看起来像:

[[m,n],[m,n],[m,n]]
Run Code Online (Sandbox Code Playgroud)

我尝试创建以下代码但它没有按预期工作:

List_3 = []
for i in range(len(List_2)):
    for j in range(len(List_1)):
         if List_2[i][1] > List_1[j][1]:
                List_3.append(List_2[i][1])   
print(List_3) 
Run Code Online (Sandbox Code Playgroud)

我怎么能解决这个问题,这样我就可以在List_2 [m] [n]中的n大于List_1 [m] [n]时创建List_3?

任何有关如何处理此问题的想法或建议将不胜感激.

python list python-3.x

2
推荐指数
1
解决办法
1429
查看次数

标签 统计

python ×4

python-3.x ×4

jupyter-notebook ×1

list ×1