2017-10-29 1 views
0

Ich habe eine lange Zeichenfolge, die ich in eine Datei speichern möchte. Wörter sind durch Leerzeichen getrennt. Die Summe der Wörter im langen String ist durch 3 teilbar.So teilen Sie die Zeichenfolge so, dass sie weniger als n Zeichen mit einer Drehung enthält

Im Grunde bin ich auf der Suche nach einer Möglichkeit, String in Stücke zu teilen. Jedes Stück ist kleiner als n Zeichen und die Anzahl der Wörter in Stücken ist ebenfalls durch 3 teilbar.

z.B.

>>> longstring = "This is a very long string and the sum of words is divisible by three" 
>>> len(longstring.split()) 
>>> 15 

sagen max Leitungslänge beträgt n = 30:

>>>split_string(longstring, 30) 
['This is a very long string', 'and the sum of words is', 'divisible by three'] 

Zur Übersicht:

  1. Die Regeln sind: Keine Zeile länger als n Zeichen.
  2. Eine Wendung ist, dass jede neue Zeile mehrere Wörter enthalten muss.

Bisher habe ich versucht TextWrap verwenden, aber ich weiß nicht, wie 2.

import textwrap  
textwrap.fill(long_line, width=69) 
+0

Ich bin nicht davon überzeugt, dass Ihre zweite Bedingung immer möglich ist. Wie würden Sie eine Zeile mit 10 Wörtern in Zeilen von Vielfachen von 3 teilen? Sind Eingaben garantiert, die ein Vielfaches von 3 sind? –

+0

Die Gesamtzahl der Wörter in einer Zeichenfolge ist durch 3 teilbar. – ad1v7

+0

Ich würde Ihre Zeichenfolge in Wörter aufteilen, dann für jedes Wort testen, ob es den aktuellen Satz zu lang machen würde. Ist dies der Fall, fixiere den aktuellen Satz und verwende das Wort als Anfang der nächsten Zeile. –

Antwort

1

zu implementieren Wenn Sie sicher sind, dass die Gesamtzahl der Wörter in einem String immer durch 3 teilbar sein, Sie können so etwas tun:

import sys 
#long string; 84 words; divisible by 3 
longString = "The charges are still sealed under orders from a federal judge. Plans were prepared Friday for anyone charged to be into custody as soon as Monday, the sources said. It is unclear what the charges are. A spokesman for the special counsel's office declined to comment. The White House also had no comment, a senior administration official said Saturday morning. A spokesman for the special counsel's office declined to comment. The White House also had no comment, a senior administration official said Saturday morning." 
#convert string to list 
listOfWords = longString.split() 
#list to contain lines 
lines = [] 
#make sure number of words is divisible by 3 
if len(listOfWords) % 3 != 0: 
    #exit 
    print "words number is not divisible by 3" 
    sys.exit() 
#keep going until list is empty 
while listOfWords: 

    i = 0 
    line = "" 
    #loop for every line 
    while True: 
     #puts the next 3 words into a string 
     temp = " ".join(listOfWords[i:i+3]) 
     #check new length of line after adding the new 3 words, if it is still less than 70, add the words, otherwise break out of the loop 
     if len(line) + len(temp) > 70: 
      break 
     line += "{} ".format(temp) 
     i+=3 
    #remove finished words from the list completely 
    listOfWords = listOfWords[i:] 
    #adds line into result list 
    lines.append(line.strip()) 

#to make sure this works 
for line in lines: 
    print len(str(line)) 
    print "Number of words: {}".format(len(line.split())) 
    print "number of chars: {}".format(len(line)) 
    print line 
    print "----------------------------------------" 
Verwandte Themen