Home > Mobile >  convert a string of 1000 characters to a list which satisfies a condition such that each list item i
convert a string of 1000 characters to a list which satisfies a condition such that each list item i

Time:11-18

I want to convert a string of 1000 characters to a list which satisfies a condition such that each list item is <256 and ends with a full stop '.' in python.That is a full legitimate sentence. So I can use each list item as as an input of dialog flow .

CodePudding user response:

yourString = "here is. your 1000. characters string."
sentences = yourString.split(".")
for index, value in enumerate(sentences):
    sentences[index]  = "."
listItem = []
item = ""
for sentence in sentences:
    tmp_item = item
    tmp_item  = sentence
    if len(tmp_item) >= 256:
        listItem.append(item)
        item = sentence
    else:
        item = tmp_item
print(listItem)

Here you go.

If it's not manadatory to keep the dots, you can simply eliminate lines 3 and 4

CodePudding user response:

Can anyone check this code. I want to write a code which takes a 256 plus long sentence as input and split it into legitimate sentences with less than 256 characters.

sample = "hello.hjvgdhvgsjdvgjsguagsiugsaucgsaicgsaugcusagcsacppppp" \
         "jdsvcjdsvcjdsgggggggvgj.vgjdvjdvdvgdjvdjvdvdh.vdvdvdvdvdv" \
         "hdvuds.vudvhduv.hudsvhdssssss.ssshivdssssssssssssiv.dsivdsvhhvd" \
         "vcjdvhdsssssssssss.sssssss.sssssssss.sssssssssssss.sss.ssssssssss" \
         "vuhduhvusssssssssss.ssssssssssssss.sssssfhgfhfsssssssssssshellothereello.hjvgdhvgsjdvgjsguagsiugsaucgsaicgsaugcusagcsacppppp" \
         "jdsvcjdsvcjdsgggggggvgj.vgjdvjdvdvgdjvdjvdvdh.vdvdvdvdvdv" \
         "hdvuds.vudvhduv.hudsvhdssssss.ssshivdssssssssssssiv.dsivdsvhhvd" \
         "vcjdvhdsssssssssss.sssssss.sssssssss.sssssssssssss.sss.ssssssssss" \
         "vuhduhvusssssssssss.sssssssssssssssssssfhgfhfsssssssssssshel"

list1=[]
while len(sample)>256:
        first=sample[0:256]
        new_index=first.rfind('.')
        first=first[0:new_index]
        list1.append(first)
        sample=sample[new_index 1:]
else :
    sample=sample[0:sample.rfind('.')]
    list1.append(sample)

for i in list1:
    print(len(i))
  • Related