如何获取字符串python的连续字数

问题描述 投票:3回答:4

我正在尝试制作一个python脚本,它接受一个字符串并给出连续单词的计数。让我们说:

string = " i have no idea how to write this script. i have an idea."

output = 
['i', 'have'] 2
['have', 'no'] 1
['no', 'idea'] 1
['idea', 'how'] 1
['how', 'to'] 1
['to', 'write'] 1
...

我正在尝试使用python而不从集合中导入集合,计数器。我的内容如下。我正在尝试使用re.findall(#whatpatterndoiuse, string)迭代字符串并进行比较,但我很难搞清楚如何操作。

string2 = re.split('\s+', string. lower())
freq_dict = {} #empty dictionary
for word in word_list:
    word = punctuation.sub("", word)
    freq_dic[word] = freq_dic.get(word,0) + 1

freq_list = freq_dic.items()
freq_list.sort()
for word, freq in freq_list:
    print word, freq

使用我不想要的收藏品。此外,它产生的输出格式不是我上面提到的格式。

import re
from collections import Counter
words = re.findall('\w+', open('a.txt').read())
print(Counter(zip(words,words[1:])))
python word-count
4个回答
2
投票

没有拉链解决这个问题非常简单。只需构建每对单词的元组并在dict中跟踪它们的计数。只有几个特殊情况需要注意 - 当输入字符串只有一个单词时,以及当你在字符串的末尾时。

试一试:

def freq(input_string):
    freq = {}
    words = input.split()
    if len(words) == 1:
        return freq

    for idx, word in enumerate(words):
        if idx+1 < len(words):
            word_pair = (word, words[idx+1])
            if word_pair in freq:
                freq[word_pair] += 1
            else:
                freq[word_pair] = 1

    return freq

1
投票

你需要解决三个问题:

  1. 生成所有单词对(['i', 'have']['have', 'no'],...);
  2. 计算这对词的出现次数;
  3. 从最常见到最不常见的对排序。

第二个问题可以通过使用Counter轻松解决。 Counter对象还提供了most_common()方法来解决第三个问题。

第一个问题可以通过多种方式解决。最紧凑的方式是使用zip

>>> import re
>>> s = 'i have no idea how to write this script. i have an idea.'
>>> words = re.findall('\w+', s)
>>> pairs = zip(words, words[1:])
>>> list(pairs)
[('i', 'have'), ('have', 'no'), ('no', 'idea'), ...]

把所有东西放在一起:

import collections
import re

def count_pairs(s):
    """
    Returns a mapping that links each pair of words
    to its number of occurrences.
    """
    words = re.findall('\w+', s.lower())
    pairs = zip(words, words[1:])
    return collections.Counter(pairs)

def print_freqs(s):
    """
    Prints the number of occurrences of word pairs
    from the most common to the least common.
    """
    cnt = count_pairs(s)
    for pair, count in cnt.most_common():
        print list(pair), count

编辑:我刚才意识到我不小心读了“带有集合,计数器......”而不是“没有导入集合,......”。我的坏,对不起。


0
投票
string = "i have no idea how to write this script. i have an idea."
def count_words(string):
    ''' warning, won't work with a leading or trailing space,
    though all you would have to do is check if there is one, and remove it.'''
    x = string.split(' ')
    return len(x)

0
投票

我想出答案发布在下面。 :)。它需要一个TXT文件,但可以轻松地操作以接收字符串。简单删除arg1并插入自己的字符串!!!

script, arg1 = argv #takes 2 arguments
#conditions
try:
        sys.argv[1]
except IndexError:
        print('doesnt work insert 2 arguments\n')
        exit()



with open(arg1, 'r') as content_file: #open file
        textsplit = content_file.read() #read it
        textsplit = textsplit.lower() #lowercase it
word_list = textsplit.split() #split file put into var word_lists

textsplit = re.sub(r"[^\w\s]+", "", textsplit).split() #remove white space

#print textsplit

freq_dic = {} #creates empty dictionary

for i in range( 0, len(textsplit)-1): #counter to itterate
        key = textsplit[i] + ',' + textsplit[i+1] # produces corresponding keys
        try:
                freq_dic[key]+=1 #if
        except:
                freq_dic[key]=1 #if not

for word in freq_dic:

        print [word], freq_dic[word]
© www.soinside.com 2019 - 2024. All rights reserved.