Функция для подсчета хэштегов - PullRequest
2 голосов
/ 21 октября 2019

Я пытаюсь получить функцию, которая считает и показывает хэштеги списка.

Пример ввода:

["Hey, im in the #pool",
 "beautiful #city",
 "#city is nice",
 "Have a nice #weekend",
 "#weekend <3",
 "Nice #"]

Вывод:

{"pool" : 1, "city" : 2, "weekend" : 2}

Но если есть только #, за которыми нет слов, он не должен учитываться как хэштег,То же самое с вещами до хэштега, что-то вроде «% #» не может считаться хэштегом. Хэштеги определены (az, AZ, 0-9), каждый второй символ заканчивает хэштегом

Мой текущий код:

def analyze(posts):
    tag = {}
    for sentence in posts:
        words = sentence.split(' ')
        for word in words:
            if word.startswith('#'):
                if word[1:] in tag.keys():
                    tag[word[1:]] += 1
                else:
                    tag[word[1:]] = 1
    return(tag)


posts = ["Hey, im in the #pool",
         "beautiful #city",
         "#city is nice",
         "Have a nice #weekend",
         "#weekend <3",
         "Nice #"]
print(analyze(posts))

Ответы [ 4 ]

8 голосов
/ 21 октября 2019

За один проход с регистронезависимым поиском регулярных выражений и collections.Counter объектом:

from collections import Counter
import re

lst = ["Hey, im in the #pool", "beautiful #city", "#city is nice",
       "Have a nice #weekend", "#weekend <3", "Nice #"]

hash_counts = Counter(re.findall(r'#([a-z0-9]+)', ' '.join(lst), re.I))
print(dict(hash_counts))

Выходными данными:

{'pool': 1, 'city': 2, 'weekend': 2}
4 голосов
/ 21 октября 2019

Используйте re с collections.Counter:

import re
from collections import Counter

data  = [ "Hey, im in the #pool",
  "beautiful #city",
  "#city is nice",
  "Have a nice #weekend",
  "#weekend <3",
  "Nice #" ]

count_hashtag = Counter()
for element in data:
    for hast_tag in re.findall('#(\w+)', element):
        count_hashtag[hast_tag] += 1

print(count_hashtag)
# Counter({'city': 2, 'weekend': 2, 'pool': 1})

Если хотите, чтобы #City и #city равнялись:

  count_hashtag[hast_tag.casefold()] += 1
0 голосов
/ 21 октября 2019
l = ["Hey, im in the #pool",
 "beautiful #city",
 "#city is nice",
 "Have a nice #weekend",
 "#weekend <3",
 "Nice #"]


from collections import defaultdict 
def func(l):

    dic = defaultdict(int)
    for i in l:
        for j in i.split():
            if j[0]=='#' and len(j)>1:
                dic[j[1:]]+=1
    return dict(dic)

print(func(l)) 

выход

{'pool': 1, 'city': 2, 'weekend': 2}
0 голосов
/ 21 октября 2019

Используйте это:

a = [ "Hey, im in the #pool",
  "beautiful #city",
  "#city is nice",
  "Have a nice #weekend",
  "#weekend <3",
  "Nice #"]
resdict = {}
for item in a:
    for word in item.split():
        if word.startswith('#') and len(word) != 1:
            if word.replace('#', '') not in resdict:
                resdict[word.replace('#', '')] = 1
            else: resdict[word.replace('#', '')] += 1
print(resdict)
...