Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 打印计数表中的重复项_Python 3.x_List_For Loop - Fatal编程技术网

Python 3.x 打印计数表中的重复项

Python 3.x 打印计数表中的重复项,python-3.x,list,for-loop,Python 3.x,List,For Loop,我想数一数复制品并打印在表中 但是表正在迭代。我如何解决这个作业 这是我的密码 dummyString = "kamu makan makan jika saya dan dia??" lists = [] def message(userInput): punctuation = "!@#$%^&*()_+<>?:.,;/" words = userInput.lower().split() conjunction = file.read().sp

我想数一数复制品并打印在表中

但是表正在迭代。我如何解决这个作业

这是我的密码

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

我所做的是从dummyString中删除标点,找到单词计数并在数据框中显示它们

以下代码应适用于您:

导入字符串
作为pd进口熊猫
从收款进口柜台
dummyString=“卡姆·马坎·马坎·吉卡·萨亚和迪亚??”
dummyString_new=dummyString.translate(str.maketrans(“”,,,string.标点))
words=dummyString_new.split()
字数=计数器(字)
df=pd.DataFrame.from_dict(wordCount,orient='index').reset_index()
df.columns=['No Kata','Frekuensi']
df.index+=1#从1而不是0开始索引。
输出:

df:

No Kata Frekuensi
1卡姆1
2马坎2
3吉卡1
4莎娅1
5对1
6直径1

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

words=“卡姆·马坎·马坎·吉卡·萨亚和迪亚??”
标点符号=“!@$%^&*()\+?:,;/”
对于标点符号中的p:
如果p大写:
单词=单词。替换(p',单词。计数(p))
words=words.split()
您可以将
set
.count
sorted
结合使用,以按降序打印单词和丰度:

w_unq=sorted((item,words.count(item)),key=lambda x:x[1],reverse=True)
打印('No.\tWord\tAbundance')
对于枚举中的i,u(w_unq):
打印({}\t{}\t{})。格式(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
函数,告诉它按照要排序的iterable的每个元素的第二个元素(在索引1处;
x[1]
)进行排序。就你的具体情况而言,这是某个词的出现次数。我可以再问你一次吗?这是关于那个任务的。我希望我的代码接受一个输入,输入直到输入空字符串才停止,并且表将在最终输出中打印,该输出存储所有输入,假设您想执行类似操作。基本上,在
while
循环中调用
input
(如果
input
的返回长度为零,则将
input
中断),将输入附加到列表中,然后清理列表中的单词,进行排序和打印。这是我最新代码的新问题谢谢,但是我的作业有一个规则,不使用字典和计数器,对不起,我的问题不是很清楚,我的作业有一个规则,但是谢谢你的回答
No.     Word    Abundance
1       makan   2
2       saya    1
3       dan     1
4       dia     1
5       jika    1
6       kamu    1