【问题标题】:print counting duplicate in table在表格中打印重复计数
【发布时间】:2023-04-10 18:50:01
【问题描述】:

我想计算重复并在表格中打印,

但表迭代。我该如何解决这个任务

这是我的代码

dummyString = "kamu makan makan jika saya dan dia??"
lists = []

def message(userInput):
    punctuation = "!@#$%^&*()_+<>?:.,;/"
    words = userInput.lower().split()
    conjunction = file.read().split("\n")
    removePunc = [char.strip(punctuation) for char in words if char not in conjunction]
    global lists
    lists = removePunc
    return removePunc

def counting(words):
    already_checked = []
    for char in words:
    # Do not repeat the words
        if char not in already_checked:
        # Check all the indices of the word in the list
            indices = [key for key, value in enumerate(words) if value == char]
            countsDuplicate = len(indices)
            table(lists, countsDuplicate)
        already_checked.append(char)

    return indices

def table(allWords, counts):
    print("Distribusi Frekuensi Kata: ")
    print("-"*70)
    print("{:>0s} {:<15s} {:<15s}".format("No","Kata","Frekuensi"))
    print("-"*70)
    words = set(allWords)
    count = 1
    for word in words:
        print("{:>0s} {:<20s} {:<10s}".format(str(count), word, str(counts)))
        count += 1

我想要这样的输出,但是表格重复了很多次

----------------------------------------------------------------------
No Kata            Frekuensi
----------------------------------------------------------------------
1 makan                2
2 dia                  1
3 kamu                 1
4 saya                 1

【问题讨论】:

    标签: python-3.x list for-loop


    【解决方案1】:

    假设您的单词列表已经清理完毕,例如

    words = "kamu makan makan jika saya dan dia??"
    punctuation = "!@#$%^&*()_+<>?:.,;/"
    for p in punctuation:
        if p in words:
            words = words.replace(p, '', words.count(p))
    words = words.split()
    

    您可以将set.countsorted 结合使用,以降序打印单词和丰度:

    w_unq = sorted(((item, words.count(item)) for item in set(words)), key=lambda x: x[1], reverse=True)
    print('No.\tWord\tAbundance')
    for i, u in enumerate(w_unq):
        print('{}\t{}\t{}'.format(i+1, *u))
    

    给你

    No.     Word    Abundance
    1       makan   2
    2       saya    1
    3       dan     1
    4       dia     1
    5       jika    1
    6       kamu    1
    

    【讨论】:

    • 我还是个新人学习 python,所以我不知道如何使用 lambda,但它可以工作,非常感谢!!
    • 乐于助人!那里有很多关于 Python 的 lambda 的教程,只需将其破解到谷歌中......在这里,它被传递给 sorted 函数,告诉它按第二个元素(在索引 1;x[1])排序要排序的迭代的每个元素。在您的具体情况下,这是某个单词的出现次数。
    • 我可以再问你一次吗?这是关于那个任务的。我希望我的代码接受一个输入,输入在我输入空字符串之前不会停止,并且表格将打印在存储所有输入的最终输出中
    • 我想你想做类似this的事情。基本上在while循环中调用input(如果input的返回例如长度为零,则break),将输入附加到列表中,然后清理列表中的单词,进行排序和印刷。
    • 这是我的新问题,最新代码stackoverflow.com/q/58398538/12208516
    【解决方案2】:

    我所做的是从 dummyString 中删除标点符号,找到字数并将它们显示在数据框中。

    下面的代码应该适合你:

    import string
    import pandas as pd
    from collections import Counter
    
    dummyString = "kamu makan makan jika saya dan dia??"
    dummyString_new=dummyString.translate(str.maketrans('', '', string.punctuation))
    
    words = dummyString_new.split()
    wordCount = Counter(words)
    
    df = pd.DataFrame.from_dict(wordCount, orient='index').reset_index()
    df.columns=['No Kata','Frekuensi']
    df.index += 1                         # to start your index from 1 and not 0.
    

    输出:

    df:

        No Kata Frekuensi
    1   kamu       1
    2   makan      2
    3   jika       1
    4   saya       1
    5   dan        1
    6   dia        1
    

    【讨论】:

    • 谢谢,但我的作业有一个规则,不使用字典和计数器,抱歉我的问题不是很清楚,我的作业有一个规则,但谢谢你的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2017-08-20
    • 1970-01-01
    相关资源
    最近更新 更多