如果重复n次,我如何删除列表中的所有字母。其中n是1-10之间的数字

时间:2017-03-05 22:37:54

标签: list python-3.x for-loop

这是清单。

list1 =['F', 'L', 'Y', 'W', 'B', 'E', 'G', 'A', 'L', 'K', 'R', 'U', 'B', 'E', 'T', 'L', 'H', 'G', 'E', 'C', 'K', 'Y', 'U', 'B', 'H', 'L', 'U', 'G', 'A', 'F', 'K', 'Y', 'F', 'M', 'P', 'U', 'B', 'K', 'F', 'G', 'I', 'O', 'N', 'S', 'Y']

我想删除重复 n 次数的字母。在此问题的上下文中, n 为4。

这是我到目前为止所尝试过的。

n = 4
alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
i = 0
for x in range(len(alphabet)-1):
    print(alphabet[i])
    h = list1.count(alphabet[x])
    print("h: ",h)
    if h == n:
        while alphabet[x] in alphabet:
            alphabet.remove(alphabet[x])
print(alphabet)

我收到错误,说明list.remove(x):x不在列表中

2 个答案:

答案 0 :(得分:2)

  

删除重复 n 次数的字母

使用collections.Counter子类的解决方案:

import collections

n = 4
list1 =['F', 'L', 'Y', 'W', 'B', 'E', 'G', 'A', 'L', 'K', 'R', 'U', 'B', 'E', 'T', 'L', 'H', 'G', 'E', 'C', 'K', 'Y', 'U', 'B', 'H', 'L', 'U', 'G', 'A', 'F', 'K', 'Y', 'F', 'M', 'P', 'U', 'B', 'K', 'F', 'G', 'I', 'O', 'N', 'S', 'Y']
counts = collections.Counter(list1)
list1 = [l for l in list1 if l in counts and counts[l] != n]

print(list1)

输出:

['W', 'E', 'A', 'R', 'E', 'T', 'H', 'E', 'C', 'H', 'A', 'M', 'P', 'I', 'O', 'N', 'S']

答案 1 :(得分:0)

不建议全新的解决方案,您可以将代码修改为:

 static class Action4 implements ActionListener {

    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {

        String name = ((JTextField) e.getSource()).getText();

        if (name.equals("Test1")) {
            name = JOptionPane.showInputDialog("Enter Name ");

            String day;
            int totalCost;
            int visitors;

            day = JOptionPane.showInputDialog("Enter what day you'd like to attend ");

            visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting "));

            totalCost = visitors * 20;

            JOptionPane.showMessageDialog(null, "  You are attending the  " + name + "  On  " + day + visitors + "  attending  " + "total cost " + totalCost);
        } else {

            if (name.equals("test2")) {
                name = JOptionPane.showInputDialog("Enter Name ");

                String day;
                day = JOptionPane.showInputDialog("Enter what day you'd like to attend ");
                int visitors;
                visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting "));

                int totalCost;
                totalCost = visitors * 17;

                JOptionPane.showMessageDialog(null, "  You are attending the  " + name + "  On  " + day + visitors + "  attending  " + "total cost " + totalCost);
            } else {

                if (name.equals("test3")) {
                    name = JOptionPane.showInputDialog("Enter Name ");

                    String day;
                    day = JOptionPane.showInputDialog("Enter what day you'd like to attend ");
                    int visitors;
                    visitors = Integer.parseInt(JOptionPane.showInputDialog("Enter how many people are visiting "));

                    int totalCost;
                    totalCost = visitors * 22;

                    JOptionPane.showMessageDialog(null, "  You are attending the  " + name + "  On  " + day + visitors + "  attending  " + "total cost " + totalCost);
                } else {

                    JOptionPane.showMessageDialog(null, "Wrong input!");

                }
            }

你的代码的问题在于你有一个while循环,如果只有一个字母h = 4(元素和所有前向字母),则从字母表中删除许多元素。这是由:

引起的
for x in alphabet:
    print(x)
    h = list1.count(x)
    print("h: ",h)
    if h == n:
        while x in alphabet:
            alphabet.remove(x)
print(alphabet)

当你删除字母[x]时,下一个元素变成字母[x](索引必须是连续的),所以while循环删除一个字母和所有前向字母。

但是,如果你想删除列表中的字母而不是字母,你应该修改:

while alphabet[x] in alphabet:
        alphabet.remove(alphabet[x])