I have a question about how to extract the shortest word from the Python list

Asked 2 years ago, Updated 2 years ago, 23 views

words = [[['Portuguese', ['Eu', 'falo', 'Português', 'muito', 'bem.']],
['English', ['I', 'speak', 'Portuguese', 'very', 'well.']]]

I'd like to extract the shortest word used in this list

If it's min (words), I'll show you the shortest list [English, ["I", "speak", "Portuguese", "very", "well"] It's coming outㅠ<

Since "I" is the shortest word, I want to get "I" How do I make the code?ㅠ<

python

2022-09-22 16:11

2 Answers

The parentheses [ and pairs do not seem to match the data you wrote down, so let's spread the data beautifully first.

words = [
  ['Portuguese', ['Eu', 'falo', 'Português', 'muito', 'bem.']],
  ['English', ['I', 'speak', 'Portuguese', 'very', 'well.']]  
]

If you open the data above, you have one of the highest lists ([]), and it has two lists that have a country and a word.

respectively ["Portuguese", ["Eu", "falo", "Portuguue',s", "muito", "mem.]] ['English', ['I', 'speak', 'Portuguese', 'very', 'well.']].

The data you are looking for is the shortest word. In order to find it, you need to tour the entire area because the data is superimposed.

You can slowly follow along while solving the overlapped list one by one.

Click the Run link in the source below to follow.

words = [
  ['Portuguese', ['Eu', 'falo', 'Português', 'muito', 'bem.']],
  ['English', ['I', 'speak', 'Portuguese', 'very', 'well.']]  
]

min_word = None

For lss in words: # Touring the top list
  Forls in lss: # Touring the language/word list
    if type(ls) is not list: 
      pass # Ignore country name    
    else: 
      For word in ls: # Travelling around each word
        if min_word == None or len(word) < len(min_word): 
          min_word = word

print(min_word) 

p.s: I wrote it down for easy explanation, but it's not a good code.


2022-09-22 16:11

To select the shortest length of words from the list, you might want to create a function separately.

Therefore, choose the smallest length string using the repetitive statement as Jungwon answered, or define a function (minlength function that selects the shortest word in the list using lambda and reduce functions as follows:

from functionools import redox #python3 must be added.

def minlength(words):
    '''
    return reduce(lambda w1, w2: w1 if len(w1)<len(w2) else w2, words)

The following is an example of an execution using minlength above.

from functionools import redox #python3 must be added.

def minlength(words):
    "@paramwordswordlist"
    return reduce(lambda w1, w2: w1 if len(w1)<len(w2) else w2, words)    

words = [
  ['English', ['speak', 'Portuguese', 'very', 'well.','I']],
  ['Portuguese', ['Eu', 'falo', 'Português', 'muito', 'bem.', 'Abc']]
]

# Lists the results of performing minlength (x[1]) for all elements x of words.
minwords = list(map(lambda x: minlength(x[1]), words))
# Minwords is the shortest word in the list of words by country.
# Re-select the shortest word among them.
print(minlength(minwords))


2022-09-22 16:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.