Я хочу найти длину каждого моего слова в текстовом файле - PullRequest
0 голосов
/ 21 апреля 2020

Я пытаюсь найти длину слова по отдельности в моем текстовом файле. Я попробовал это с помощью следующего кода, но этот код показывает мне количество слов, сколько раз это слово используется в файле.

text = open(r"C:\Users\israr\Desktop\counter\Bigdata.txt") 
d = dict() 

for line in text: 
    line = line.strip() 
    line = line.lower()
    words = line.split(" ") 

    for word in words: 
        if word in d: 
            d[word] = d[word] + 1
        else: 
            # Add the word to dictionary with count 1 
            d[word] = 1

for key in list(d.keys()): 
    print(key, ":", d[key])

И вывод выглядит примерно так:

china : 14
emerged : 1
as : 16
one : 5
of : 44
the : 108
world's : 7
first : 2
civilizations, : 1
in : 26
fertile : 1
basin : 1
yellow : 1
river : 1
north : 1
plain. : 1

В основном, я хочу список слов одинаковой длины, например, china, first, world: 5 this 5 - длина всех этих слов и т. Д. Для слов различной длины в другом списке

Ответы [ 3 ]

1 голос
/ 21 апреля 2020

Если вам нужна общая длина всех слов отдельно, вы можете найти их по следующей формуле:

len(word) * count(word) для всех word в words

равно в python: d[key] * len(key)

Измените последние 2 строки следующим образом:

for key in list(d.keys()):
    print(key, ":", d[key] * len(key))

---- РЕДАКТИРОВАТЬ ---- Я думаю, это то, что вы спрашивали в комментариях. Ниже приведен код группы, члены которой имеют одинаковую длину.

    for word in words:
        if len(word) in d:
            if word not in d[len(word)]:
                d[len(word)].append(word)
        else:
            # Add the word to dictionary with count 1
            d[len(word)] = [word]

for key in list(d.keys()):
    print(key, ":", d[key])

Вывод этого кода:

3 : ['the', 'bc,', '(c.', 'who', 'was', '100', 'bc)', 'and', 'xia', 'but', 'not', 'one', 'due', '8th', '221', 'qin', 'shi', 'for', 'his', 'han', '220', '206', 'has', 'war', 'all', 'far']
8 : ['earliest', 'describe', 'writings', 'indicate', 'commonly', 'however,', 'cultural', 'history,', 'regarded', 'external', 'internal', 'culture,', 'troubled', 'imperial', 'selected', 'replaced', 'republic', 'mainland', "people's", 'peoples,', 'multiple', 'kingdoms', 'xinjiang', 'present.', '(carried']
5 : ['known', 'china', 'early', 'shang', 'texts', 'grand', 'ruled', 'river', 'which', 'along', 'these', 'arose', 'years', 'their', 'rule.', 'began', 'first', 'those', 'huang', 'title', 'after', 'until', '1912,', 'tasks', 'elite', 'young', '1949.', 'unity', 'being', 'civil', 'parts', 'other', 'world', 'waves', 'basis']
7 : ['written', 'records', 'history', 'dynasty', 'ancient', 'century', 'mention', 'writing', 'period,', 'xia.[5]', 'valley,', 'chinese', 'various', 'centers', 'yangtze', "world's", 'cradles', 'concept', 'mandate', 'justify', 'central', 'country', 'smaller', 'period.', 'another', 'warring', 'created', 'himself', 'huangdi', 'marking', 'systems', 'enabled', 'emperor', 'control', 'routine', 'handled', 'special', 'through', "china's", 'between', 'periods', 'culture', 'western', 'foreign']
2 : ['of', 'as', 'wu', 'by', 'no', 'is', 'do', 'in', 'to', 'be', 'at', 'or', 'bc', '21', 'ad']
4 : ['date', 'from', '1250', 'bc),', 'king', 'such', 'book', '11th', '(296', 'held', 'both', 'with', 'zhou', 'into', 'much', 'qin,', 'fell', 'soon', '(206', 'ad).', 'that', 'vast', 'were', 'men,', 'last', 'qing', 'then', 'most', 'whom', 'eras', 'have', 'some', 'asia', 'form']
9 : ['1600–1046', 'mentioned', 'documents', 'chapters,', 'historian', '2070–1600', 'existence', 'neolithic', 'millennia', 'thousands', '(1046–256', 'pressures', 'following', 'developed', 'conquered', '"emperor"', 'beginning', 'dynasties', 'directly.', 'centuries', 'carefully', 'difficult', 'political', 'dominated', 'stretched', 'contact),']
6 : ['during', "ding's", '(early', 'bamboo', 'annals', 'before', 'shang,', 'yellow', 'cradle', 'river.', 'shang.', 'oldest', 'heaven', 'weaken', 'states', 'spring', 'autumn', 'became', 'warred', 'times.', 'china.', 'death,', 'peace,', 'failed', 'recent', 'steppe', 'china;', 'tibet,', 'modern']
12 : ['reign,[1][2]', 'twenty-first', 'longer-lived', 'bureaucratic', 'calligraphy,', '(1644–1912),', '(1927–1949).', 'occasionally', 'immigration,']
11 : ['same.[3][4]', 'independent', 'traditional', 'territories', 'well-versed', 'literature,', 'philosophy,', 'assimilated', 'population.', 'warlordism,']
10 : ['historical', 'originated', 'continuous', 'supplanted', 'introduced', 'government', 'eventually', 'splintered', 'literature', 'philosophy', 'oppressive', 'successive', 'alternated', 'influences', 'expansion,']
1 : ['a', '–']
13 : ['civilization.', 'civilizations', 'examinations.', 'statehood—the', 'assimilation,']
17 : ['civilizations,[6]']
16 : ['civilization.[7]']
0 : ['']
14 : ['administrative']
18 : ['scholar-officials.']

Ниже приведена полная версия кода.

text = open("bigdata.txt")
d = dict()

for line in text:
    line = line.strip()
    line = line.lower()
    words = line.split(" ")

    for word in words:
        if len(word) in d:
            if word not in d[len(word)]:
                d[len(word)].append(word)
        else:
            d[len(word)] = [word]

for key in list(d.keys()):
    print(key, ":", d[key])
0 голосов
/ 21 апреля 2020

Вы можете создать список длин слов и затем обработать их через встроенный счетчик python:

from collections import Counter

with open("mytext.txt", "r") as f:
    words = f.read().split()
    words_lengths = [len(word) for word in words]
    counter = Counter(words_lengths)

Вывод будет выглядеть как:

In[1]:counter
Out[1]:Counter({7: 146, 9: 73, 5: 73, 4: 146, 1: 73})

Где ключи - это длина слов, а значения - количество раз, когда они встречались.

Вы можете работать с этим, как с обычным словарем.

0 голосов
/ 21 апреля 2020

Когда вы посмотрите на код для работы с каждым словом, вы увидите свою проблему ..

for word in words: 
        if word in d: 
            d[word] = d[word] + 1
        else: 
            # Add the word to dictionary with count 1 
            d[word] = 1

Здесь вы проверяете, есть ли слово в словаре. Если это так, добавьте 1 к его ключу, когда мы его найдем. Если это не так, инициализируйте его на 1. Это основная концепция для подсчета повторений.

Если вы хотите посчитать длину слова, вы можете просто сделать.

for word in words: 
        if word not in d: 
            d[word] = len(word)

И чтобы вывести свой диктат, вы можете сделать

for k, v in d.items():
    print(k, ":", v)
...