从列表中删除重复的子列表

时间:2016-06-05 14:49:59

标签: r

我有两个相同的子列表,并希望消除那些重复的列表并以列表格式提供结果。是否有任何功能可以执行此操作,如交叉或唯一应用于矢量。我知道我可以取消列表然后使用unique,然后再对前一个结构重新进行重新设置,但是想知道是否有一个函数可以对列表执行相同的操作而不是向量。我也知道我可以使用reduce来简化列表,然后通过@akrun应用下面的sugestion。但是,我正在寻找解决方案,以便能够处理具有复杂子列表的列表。

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Tester {

    private JFrame frame;
    private JPanel panel;
    private JTextField textField1;
    private JTextField textField2;
    private boolean eventTrigger = false;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Tester window = new Tester();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Tester() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(50, 50, 200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setLayout(new GridLayout(0,1,0,0));

        textField1 = new JTextField("Change the first.");
        textField2 = new JTextField("Change the second.");
        textField1.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                if(eventTrigger == false) {
                    trigger();
                }
            }
            public void removeUpdate(DocumentEvent e) {
                if(eventTrigger == false) {
                    trigger();
                }
            }
            public void changedUpdate(DocumentEvent e) {
            }
            public void trigger() {
                eventTrigger = true;
                textField1.addFocusListener(new FocusListener() {
                    public void focusGained(FocusEvent e) {
                    }
                    public void focusLost(FocusEvent e) {
                        System.out.println("You changed the text to: " + textField1.getText());
                        eventTrigger = false;
                    }
                });
                textField1.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("You changed the text to: " + textField1.getText());
                        eventTrigger = false;
                    }
                });
            }
        });
        textField2.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                if(eventTrigger == false) {
                    trigger();
                }
            }
            public void removeUpdate(DocumentEvent e) {
                if(eventTrigger == false) {
                    trigger();
                }
            }
            public void changedUpdate(DocumentEvent e) {
            }
            public void trigger() {
                eventTrigger = true;
                textField2.addFocusListener(new FocusListener() {
                    public void focusGained(FocusEvent e) {
                    }
                    public void focusLost(FocusEvent e) {
                        System.out.println("You changed the text to: " + textField2.getText());
                        eventTrigger = false;
                    }
                });
                textField2.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("You changed the text to: " + textField2.getText());
                        eventTrigger = false;
                    }
                });
            }
        });
        panel.add(textField1);
        panel.add(textField2);
        frame.add(panel);
    }
}

1 个答案:

答案 0 :(得分:0)

一种方法是使用duplicated返回逻辑索引,然后使用MapFilter我们将list

分组
l1 <- do.call(c, TER)
i1 <- duplicated(l1, fromLast=TRUE)|!duplicated(l1)
Filter(length, Map('[', TER, split(i1, rep(seq_along(TER), lengths(TER)))))
#[[1]]
#[[1]][[1]]
#[1] 1 2 3

#[[1]][[2]]
#[1] 1 2 3 4

假设TER是

TER <- list(list(c(1,2,3),c(1,3,3,4)),list(c(1,2,3),c(1,2,3,4)))

它给出输出

#[[1]]
#[[1]][[1]]
#[1] 1 2 3

#[[1]][[2]]
#[1] 1 3 3 4


#[[2]]
#[[2]][[1]]
#[1] 1 2 3 4