Вот несколько простых, способов сохранения порядка для разных списков строк.
Код
Необычный подход с использованием pathlib
:
import pathlib
temp1 = ["One", "Two", "Three", "Four"]
temp2 = ["One", "Two"]
p = pathlib.Path(*temp1)
r = p.relative_to(*temp2)
list(r.parts)
# ['Three', 'Four']
Это предполагает, что оба списка содержат строки с эквивалентными началами. Подробнее см. docs . Обратите внимание, что это не особенно быстро по сравнению с заданными операциями.
Прямая реализация с использованием itertools.zip_longest
:
import itertools as it
[x for x, y in it.zip_longest(temp1, temp2) if x != y]
# ['Three', 'Three', 'Four']
Если вы выполните поиск в Google: «stdout JTextArea», у вас будет несколько ссылок для решения вашей проблемы.
В последней ссылке buddybob расширяет java.io.OutputStream
для печати стандартного вывода на его JTextArea. Я добавил его решение ниже.
/*
*
* @(#) TextAreaOutputStream.java
*
*/
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
/**
* An output stream that writes its output to a javax.swing.JTextArea
* control.
*
* @author Ranganath Kini
* @see javax.swing.JTextArea
*/
public class TextAreaOutputStream extends OutputStream {
private JTextArea textControl;
/**
* Creates a new instance of TextAreaOutputStream which writes
* to the specified instance of javax.swing.JTextArea control.
*
* @param control A reference to the javax.swing.JTextArea
* control to which the output must be redirected
* to.
*/
public TextAreaOutputStream( JTextArea control ) {
textControl = control;
}
/**
* Writes the specified byte as a character to the
* javax.swing.JTextArea.
*
* @param b The byte to be written as character to the
* JTextArea.
*/
public void write( int b ) throws IOException {
// append the data as characters to the JTextArea control
textControl.append( String.valueOf( ( char )b ) );
}
}
TextAreaOutputStream
расширяет классjava.io.OutputStream
и переопределяет его перегрузку методаwrite(int)
, этот класс использует ссылку на экземпляр управленияjavax.swing.JTextArea
, а затем добавляет к нему вывод всякий раз, когда вызывается метод write (int b).Чтобы использовать класс
blockquote>TextAreaOutputStream
, [yo] u должен использовать:Использование
// Create an instance of javax.swing.JTextArea control JTextArea txtConsole = new JTextArea(); // Now create a new TextAreaOutputStream to write to our JTextArea control and wrap a // PrintStream around it to support the println/printf methods. PrintStream out = new PrintStream( new TextAreaOutputStream( txtConsole ) ); // redirect standard output stream to the TextAreaOutputStream System.setOut( out ); // redirect standard error stream to the TextAreaOutputStream System.setErr( out ); // now test the mechanism System.out.println( "Hello World" );
JTextArea
и установить выход системы в ваш классAddNumber
, чтобы ваши системные вызовы отправлялись наJTextArea
. – Mr. Polywhirl 7 November 2013 в 13:38