代码中的语法无效,我认为应该没有错误

时间:2018-03-20 11:14:47

标签: python

correct = [1 if ((a == 1 and b == 1) or (a == 0 and b == 0)) else 0 for (a, b)]


我是python世界中的新手,我发现当我运行我的文件时,程序显示

  

SyntaxError:语法无效

在上面的代码中但是据我所知,这段代码应该没有SyntaxError,所以我想问为什么,这段代码哪里出错了?我试着找到我错在哪里,但仍然我不知道这个。
我使用Anaconda3-4.2.0-Windows-x86_64这个版本

6 个答案:

答案 0 :(得分:1)

我可能错了,但您错过了in语句或在列表推导中说了迭代。

my_list = [(1, 1), (1, 0)]
correct = [1 if ((a == 1 and b == 1) or (a == 0 and b == 0)) else 0 for (a, b) in my_list]
print(correct)
out: [1, 0]

答案 1 :(得分:1)

您正在做的事情基本上是"而不是xor",因此,您的整个逻辑可以替换为int(not (a ^ b))

a,b = 0, 0
print(int(not (a ^ b)))
a, b = 1, 0
print(int(not (a ^ b)))
a, b = 0, 1
print(int(not (a ^ b)))
a, b = 1, 1
print(int(not (a ^ b)))
#  1
#  0
#  0
#  1

如果你不喜欢"魔法" "不是xor"您可以针对(0, 0)(1, 1)明确验证输入:

print(1 if (a, b) in ((0, 0), (1, 1)) else 0)


这仅取代if条件。如果你有一个元组列表,那么你仍然需要in,如其他答案所示:

def validate(a, b):
    return int(not a ^ b)

li = [(0, 0), (0, 1), (1, 0), (1, 1)]
print([validate(a, b) for a, b in li])
# [1, 0, 0, 1]

或使用map,但是需要更改validate以接受元组(如果使用Python 3并且想要的输出应该是列表,则需要调用list ):

def validate(tup):
    return int(not tup[0] ^ tup[1])

li = [(0, 0), (0, 1), (1, 0), (1, 1)]
print(list(map(validate, li)))
# [1, 0, 0, 1]

答案 2 :(得分:0)

在定义a和b后,您可以执行以下操作:

UndoRedoTransactionManager

或者采用三元格式,如下所示:

public class UndoRedoTransactionManager : IUndoRedoTransactionManager
{
    private readonly Stack<UndoRedoTransaction> _done = new Stack<UndoRedoTransaction>();
    private readonly Stack<UndoRedoTransaction> _unDone = new Stack<UndoRedoTransaction>();

    public bool CanDo => _unDone.Count > 0;
    public bool CanUndo => _done.Count > 0;

    public void ClearHistory()
    {
        _done.Clear();
        _unDone.Clear();
    }

    public bool Do(Action action, Action undoAction, string description)
    {
        var trans = new UndoRedoTransaction(action, undoAction, description);
        _unDone.Clear();
        trans.Do();
        _done.Push(trans);
        return true;
    }

    public bool Redo()
    {
        if (_unDone.Count == 0) return false;
        var act = _unDone.Pop();
        act.Do();

        _done.Push(act);
        return true;
    }

    public void RedoAll()
    {
        while (Redo())
        {
        }
    }

    public bool Undo()
    {
        if (_done.Count == 0) return false;
        var act = _done.Pop();
        act.Undo();

        _unDone.Push(act);
        return true;
    }
    public void UndoAll()
    {
        while (Undo())
        {
        }
    }

    private class UndoRedoTransaction
    {
        private readonly Action _doAction;
        private readonly Action _undoAction;

        public string Description { get; }

        public UndoRedoTransaction(Action doAction, Action undoAction, string description)
        {
            _doAction = doAction;
            _undoAction = undoAction;
            Description = description;
        }


        public void Do()
        {
            _doAction();
        }

        public void Undo()
        {
            _undoAction();
        }
    }
}

如果你想要的是在(a = 1和b = 1)或(a = 0和b = 0)时使正确= 1并且在其余可能性中使正确= 0

答案 3 :(得分:0)

这是对列表理解的严重误用。

您可能希望实现的逻辑可以通过三元语句实现:

a, b = 1, 1
correct = 1 if ((a == 1 and b == 1) or (a == 0 and b == 0)) else 0
#1

然而,即使这是接近不可读的。三元陈述没有任何内在错误;所以我建议分裂你的逻辑:

test1 = (a == 1) and (b == 1)
test2 = (a == 0) and (b == 0)

correct = 1 if (test1 or test2) else 0

答案 4 :(得分:0)

如果你只需要评估ab而你想要一个单行,你可以选择

correct = 1 if ((a == 1 and b == 1) or (a == 0 and b == 0)) else 0

如果ab代表元组列表中的几个值,并且您希望列表返回1和0,则可以使用列表解析:

correct = [1 if ((a == 1 and b == 1) or (a == 0 and b == 0)) else 0 for a,b in mylist]

答案 5 :(得分:0)

[whatever_expression for (a, b)]

确实是错误的 - 它应该(来自纯粹的语法POV)

[whatever_expression for (a, b) in some_iterable]

您真正想要做的事情目前还不清楚,但如果您有两个变量ab,并想检查两者是否等于0或两者都是等于1,您根本不需要列表理解:

correct = 1 if (a == 0 and b == 0) or (a == 1 and b == 1) else 0

因为在Python中,Boolint的子类False == 0True == 1,所以可以简化为

correct = (a == 0 and b == 0) or (a == 1 and b == 1)

如果你实际上有一个对列表并且想要测试所有这些对,那么使用列表推导才有意义,即:

pairs = [(0, 0), (0, 1), (1, 0), (1, 1)]
corrects = [(a == 0 and b == 0) or (a == 1 and b == 1) for (a, b) in pairs]

会产生

[True, False, False, True]