Reputation: 73
I want to extract the words from the biggest number which is 3 with is love to the smallest number
[1, 'hello', 2, ' word', 3, ' love', 1, ' hi', 1, ' you']
the result will be:
['love','word','hello', 'hi','you']
Thanks for the help!
I have this code but I'm stuck because of error message TypeError: sort() takes no positional arguments
:
s="hello, word, word, love, love, love, hi, you"
def f(s):
all=s.split(',')
print(all)
uni=[]
count=[]
sorted=[]
for word in all:
if word not in uni :
uni.append(word)
print(uni)
for word in uni:
c=all.count(word)
count.extend([c,word])
print(count)
count.sort(reversed)
for w in count:
sorted.append(w(1))
return sorted
f(s)
print(sorted)
Upvotes: 1
Views: 483
Reputation: 2537
You can use zip
to pair the words the numbers and then sort it
>>> lst = [1, 'hello', 2, ' word', 3, ' love', 1, ' hi', 1, ' you']
>>> [word.strip() for _,word in sorted(zip(lst[::2], lst[1::2]), reverse=True)]
['love', 'word', 'hello', 'you', 'hi']
To retain the order for words with same frequency, you have to zip
with range
as follows
>>> [w.strip() for *_,w in sorted(zip(lst[::2], range(len(lst)//2, 0, -1), lst[1::2]), reverse=True)]
['love', 'word', 'hello', 'hi', 'you']
Upvotes: 2
Reputation: 17322
what about this beautiful solutions:
1) if you want to have your list:
l = [1, 'hello', 2, ' word', 3, ' love', 1, ' hi', 1, ' you']
l[1::2] = [e.strip() for e in l[1::2]]
print([w for _, w in sorted(sorted(zip(l[::2], l[1::2])), key = lambda x: x[0], reverse=True)])
output:
['love', 'word', 'hello', 'hi', 'you']
2) if you want to start from s:
from collections import Counter
s = "hello, word, word, love, love, love, hi, you"
print([w for w, _ in sorted(sorted(Counter(s.split(', ')).items()), key=lambda x: x[1], reverse=True)])
output:
['love', 'word', 'hello', 'hi', 'you']
Upvotes: 0
Reputation: 71
Your answer is not pythonic. If you have already counted the occurrences of the words, you may try
aa = [1, 'hello', 2, ' word', 3, ' love', 1, ' hi', 1, ' you']
cc = [v for c,v in sorted(zip(aa[0::2], aa[1::2]),reverse=True)]
print(cc)
If the input is a string as in your code, the following code works.
from collections import Counter
s="hello, word, word, love, love, love, hi, you"
cnt = Counter()
for w in s.split(','):
cnt[w.strip()] += 1
print(sorted(cnt, key=cnt.get, reverse=True))
Thank @Pynchia. Now the same thing can be achieved in two lines.
from collections import Counter
cnt=Counter(s.replace(',', '').split())
print(sorted(cnt, key=cnt.get, reverse=True))
Upvotes: 0
Reputation: 11590
yet another solution, more idiomatic and a classic in grouping elements from an iterable
x = [1, 'hello', 2, ' word', 3, ' love', 1, ' hi', 1, ' you']
t = zip(*[iter(x)]*2) # creates a seq of tuples with two consecutive elements each
s = sorted(t, reverse=True) # no need for a key
out = [e[1] for e in s]
print(out)
which gives
[' love', ' word', 'hello', ' hi', ' you']
Upvotes: 0
Reputation: 4189
x = [1, 'hello', 2, ' word', 3, ' love', 1, ' hi', 1, ' you']
z = [w for _,w in sorted(zip(x[0::2],x[1::2]), reverse=True)]
print(z)
#[' love', ' word', 'hello', ' you', ' hi']
If the problem starts with s
from collections import Counter
s="hello, word, word, love, love, love, hi, you"
slist = s.split(',')
sdict = dict(Counter(slist))
result = sorted(sdict.items(), key=lambda x: x[1], reverse=True)
result = [i[0] for i in result]
print(result)
# [' love', ' word', 'hello', ' hi', ' you']
Upvotes: 1
Reputation: 41
I first thought about converting your list to a dictionary, but best would be to create a list of tuples. Obviously, the elements in the array seem sorted. Then sort and reverse the order and yiu will get your list
test = [1, 'hello', 2, ' word', 3, ' love', 1, ' hi', 1, ' you']
new_test = [(test[x], test[x+1]) for x in range(0,len(test),2)]
new_test.sort()
new_test.reverse()
new_test
strings = []
for elem in new_test:
strings.append(elem[1])
print(strings)
[' love', ' word', 'hello', ' you', ' hi']
Upvotes: 0
Reputation: 42758
Your data structure is not good. Don't put count and word in a flat list, but use tuples. Otherwise, all numbers would be sorted independently from the words.
reverse
is a keyword-argument, which expects a boolean value.
def sort_words(text):
words = text.split(',')
unique_words = set(words)
word_counts = []
for word in unique_words:
word_counts.append((words.count(word), word))
word_counts.sort(reverse=True)
sorted = []
for count, word in word_counts:
sorted.append(word)
return sorted
Upvotes: 0