Использование параметра Defaultdict:
from collections import defaultdict
def read_file(fname):
words_dict = defaultdict(int)
fp = open(fname, 'r')
lines = fp.readlines()
words = []
for line in lines:
words += line.split(' ')
for word in words:
words_dict[word] += 1
return words_dict
Вот вариант
library(tidyverse)
data %>%
gather(key, val, -query_name, -match_name) %>%
select(-match_name, -key) %>%
group_by(query_name, val) %>%
add_count() %>%
group_by(query_name) %>%
filter(n == max(n)) %>%
summarise(location_output = paste0(unique(val[!is.na(val)]), collapse = ";"))
## A tibble: 3 x 2
# query_name location_output
# <fct> <chr>
#1 feature1 NorthAmerica;UnitedStates;NewYork
#2 feature2 NorthAmerica;Canada
#3 feature3 Europe;Germany