-1

I have a course in AI and I have an exercise to write a code producing the shortest possible sentence (in term of number of character) I have a list2D of departure It's just word list no real english sentence.I do not need to have a correct sentence in English

data=[["One","Two","He","You","a"],["have","had","make","do"],["red","blue"]]

I have to make a sentence of 3 words as small as possible (in number of character), the solution for the example i presented before would be "a do red"

here is what I did in genetic algorithm, I seek the most value patite by modifying each gen 0.05% of the pop.

An individual is a sentence, A population is a phrase set , I try to generate the smallest possible sentence.

the problem is that the value does not change. Thanks in advance for your help.

rori
  • 11
  • 8

1 Answers1

2

If you have to pick one word from each list, you could do:

for sublist in data:
    print(min(sublist, key=len))

Out [4]:
a
do
red

Or if you want a single string as result:

" ".join([min(sublist, key=len) for sublist in data])

Out [2]:
'a do red'

Note that this picks the first shortest word from each list, so if there are multiple words with the shortest length you get whichever is first in your list. Rather than print them out you may want to append them to a new list, or do something else dependent on your use case.

Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
Ben
  • 856
  • 5
  • 9