Re: From JoyceUlysses.txt -- words occurring exactly once

Liste des GroupesRevenir à cl python 
Sujet : Re: From JoyceUlysses.txt -- words occurring exactly once
De : list1 (at) *nospam* tompassin.net (Thomas Passin)
Groupes : comp.lang.python
Date : 31. May 2024, 22:27:00
Autres entêtes
Message-ID : <mailman.77.1717199313.2909.python-list@python.org>
References : 1 2
User-Agent : Mozilla Thunderbird
On 5/30/2024 4:03 PM, HenHanna via Python-list wrote:
 Given a text file of a novel (JoyceUlysses.txt) ...
 could someone give me a pretty fast (and simple) Python program that'd give me a list of all words occurring exactly once?
                -- Also, a list of words occurring once, twice or 3 times
   re: hyphenated words        (you can treat it anyway you like)
         but ideally, i'd treat  [editor-in-chief]
                                [go-ahead]  [pen-knife]
                                [know-how]  [far-fetched] ...
        as one unit.
You will probably get a thousand different suggestions, but here's a fairly direct and readable one in Python:
s1 = 'Is this word is the only word repeated in this string'
counts = {}
for w in s1.lower().split():
     counts[w] = counts.get(w, 0) + 1
print(sorted(counts.items()))
# [('in', 1), ('is', 2), ('only', 1), ('repeated', 1), ('string', 1), ('the', 1), ('this', 2), ('word', 2)]
Of course you can adjust the definition of what constitutes a word, handle punctuation and so on, and tinker with the output format to suit yourself.  You would replace s1.lower().split() with, e.g., my_custom_word_splitter(s1).

Date Sujet#  Auteur
30 May 24 * From JoyceUlysses.txt -- words occurring exactly once28HenHanna
30 May 24 +* Re: From JoyceUlysses.txt -- words occurring exactly once15dn
31 May 24 i`* Re: From JoyceUlysses.txt -- words occurring exactly once14HenHanna
1 Jun 24 i +- Re: From JoyceUlysses.txt -- words occurring exactly once1Peter J. Holzer
1 Jun 24 i +- Re: From JoyceUlysses.txt -- words occurring exactly once1Thomas Passin
5 Jun 24 i +- Re: From JoyceUlysses.txt -- words occurring exactly once1dn
5 Jun 24 i +- Re: From JoyceUlysses.txt -- words occurring exactly once1Grant Edwards
5 Jun 24 i +- Re: From JoyceUlysses.txt -- words occurring exactly once1Thomas Passin
7 Jun 24 i +- Re: From JoyceUlysses.txt -- words occurring exactly once1Mats Wichmann
8 Jun 24 i +* Re: From JoyceUlysses.txt -- words occurring exactly once2Larry Martell
8 Jun 24 i i`- Re: From JoyceUlysses.txt -- words occurring exactly once1Stefan Ram
8 Jun 24 i +- Re: From JoyceUlysses.txt -- words occurring exactly once1Thomas Passin
8 Jun 24 i +- Re: From JoyceUlysses.txt -- words occurring exactly once1<avi.e.gross
8 Jun 24 i +- Re: From JoyceUlysses.txt -- words occurring exactly once1Thomas Passin
9 Jun 24 i +- Re: From JoyceUlysses.txt -- words occurring exactly once1<avi.e.gross
9 Jun 24 i `- Re: From JoyceUlysses.txt -- words occurring exactly once1Grant Edwards
31 May 24 +* Re: From JoyceUlysses.txt -- words occurring exactly once2Pieter van Oostrum
31 May 24 i`- Re: From JoyceUlysses.txt -- words occurring exactly once1Grant Edwards
31 May 24 +- Re: From JoyceUlysses.txt -- words occurring exactly once1dieter.maurer
31 May 24 +- Re: From JoyceUlysses.txt -- words occurring exactly once1Thomas Passin
1 Jun 24 `* Re: From JoyceUlysses.txt -- words occurring exactly once8Mats Wichmann
3 Jun 24  `* Re: From JoyceUlysses.txt -- words occurring exactly once7Edward Teach
3 Jun 24   +* Re: From JoyceUlysses.txt -- words occurring exactly once5Grant Edwards
4 Jun 24   i`* Re: From JoyceUlysses.txt -- words occurring exactly once4Edward Teach
4 Jun 24   i +- Re: From JoyceUlysses.txt -- words occurring exactly once1Grant Edwards
4 Jun 24   i +- Re: From JoyceUlysses.txt -- words occurring exactly once1<avi.e.gross
5 Jun 24   i `- Re: From JoyceUlysses.txt -- words occurring exactly once1Chris Angelico
4 Jun 24   `- Re: From JoyceUlysses.txt -- words occurring exactly once1dieter.maurer

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal