Home>

Help mix letters in words text nothing happens in this code

from nltk.tokenize import word_tokenize
import random
a= "some text"
a1= word_tokenize (a)
for i in a1:
    random.shuffle (i.split ())
print ('' .join (a1))

Well, logical. You don't use a shuffled array at all.

Qwertiy♦2021-02-23 12:32:55
  • Answer # 1
    import random
    text= "some text"
    words= text.split ()
    for i, word in enumerate (map (list, words)):
        random.shuffle (word)
        words [i]= '' .join (word)
    print (* words) # ->
     eosm txet
    

    Strings are immutable in Python, so to shuffle letters in words of text, each word is converted to a list of characters, the list is shuffled, then the concatenated characters are assigned back to the word list.

    In general, words can be broken into letters not only character by character (into separate Unicode code points). Cm. How do I split a string into separate characters?

  • Answer # 2

    I can suggest this option:

    import random
    slovo= 'Hello'
    slovo_list= list (slovo)
    abrakadabra= random.sample (slovo_list, len (slovo_list))
    print (abrakadabra)
    

    welcome to This Club in Russian! please leave a little more detailed answers. try to tell about all the nuances that may arise when solving the problem according to your scenario. state your suggestions. a short answer is also possible, but the more detailed and understandable it is, the better. you can complete the answer by clicking edit

    aleksandr barakin2021-02-23 14:33:47
Trends