如何将列表中的元素放在字典值的打印旁边?

问题描述 投票:0回答:1

期望的输出:

1个字母单词的比例:4.76%(1231个单词)

2字母单词比例:16.14%(4177个单词)

3字母单词比例:20.33%(5261个单词)

4个字母的单词比例:24.33%(6295个单词)

5个字母的单词比例:15.03%(3889个单词)

6字母单词比例:7.91%(2048个单词)

7个字母的单词比例:5.22%(1352个单词)

8个字母的单词比例:3.68%(953个单词)

9个字母的单词比例:1.46%(378个单词)

10个字母的单词比例:0.73%(190个单词)

11个字母的单词比例:0.27%(71个单词)

12字母单词比例:0.08%(20个单词)

13个字母的单词比例:0.04%(10个单词)

14个字母的单词比例:0.01%(2个单词)

我的代码:

file = open('romeo_and_juliet_data.txt','r')
mydict = {}
wordcount = 0
proportion = 0
for line in file:
    line = line.strip()
    line = line.split()
    for word in line:
        word = word.replace("'",'')
        wordlength = len(word)
        wordcount+= 1
        if wordlength in mydict:
            mydict[wordlength] += 1
        else:
            mydict[wordlength] =1
        for value in mydict.values():
            proportion = list(mydict.values())
for key,value in mydict.items():
    mydict[key] = round((value/wordcount)*100,2)
for key,value in mydict.items():
    mydict[key] = f'Proportion of {key}- letter words: {value}%'
for value in mydict.values():
    print(value)
print(proportion)

我的代码给我的输出:

5个字母单词比例:15.03%

三字母单词比例:20.33%

6个字母的单词比例:7.91%

1个字母单词比例:4.76%

8个字母单词比例:3.68%

10个字母的单词比例:0.73%

4个字母的单词比例:24.33%

2个字母的单词比例:16.14%

7个字母的单词比例:5.22%

13个字母的单词比例:0.04%

11个字母的单词比例:0.27%

9个字母的单词比例:1.46%

12字母单词比例:0.08%

14个字母的单词比例:0.01%

[3889, 5261, 2048, 1231, 953, 190, 6295, 4177, 1352, 10, 71, 378, 20, 2]

如何从字典值打印旁边的列表中获取元素?

我尝试这样做:

file = open('romeo_and_juliet_data.txt','r')
mydict = {}
wordcount = 0
proportion = 0
for line in file:
    line = line.strip()
    line = line.split()
    for word in line:
        word = word.replace("'",'')
        wordlength = len(word)
        wordcount+= 1
        if wordlength in mydict:
            mydict[f'Proportion of {wordlength}- letter words:'] += 1
        else:
            mydict[f'Proportion of {wordlength}- letter words:'] =1
        for value in mydict.values():
            proportion = list(mydict.values())
for key,value in mydict.items():
    mydict[key] = round((value/wordcount)*100,2)
for key,value in mydict.items():
    mydict[key] =  f'{value}%'
print(mydict)

但由于某种原因,f 字符串使 if 语句不起作用,并且它不添加计数,只是添加 1,然后停止。我不确定为什么将 mydict 键更改为 f 字符串会使其行为与仅 [wordlength] 不同

python python-3.x list dictionary iteration
1个回答
0
投票

这应该让你走上正轨......

在数据捕获中,仅存储数字,而不是整个字符串。更容易合作。

当您访问要打印的数据时,您可以对键进行排序(如图所示)以使其按顺序排列。

您可以根据所示值计算比例。

file = open('romeo_and_juliet_data.txt','r')
mydict = {}
wordcount = 0
# proportion = 0

# step 1:  gather the data...
for line in file:
    line = line.strip()
    line = line.split()
    for word in line:
        word = word.replace("'",'')
        wordlength = len(word)
        wordcount+= 1
        if wordlength in mydict:
            mydict[wordlength] += 1
        else:
            mydict[wordlength] = 1
        # for value in mydict.values():
        #     proportion = list(mydict.values())

# step 2:  process the data
# make a sorted list of the keys and use that to access the data
for key in sorted(mydict.keys()):
    count = mydict[key]
    # compute the portion "on the fly"
    proportion = round((count/wordcount)*100,2)

    # print it...
    print(f'Proportion of {count} letter words: {proportion}')
© www.soinside.com 2019 - 2024. All rights reserved.