布尔运算符不能始终如一地工作。正确打印“ a”,但错误打印“ b”

时间:2018-11-11 15:49:34

标签: python-3.x dictionary boolean

因此,我想在定义类的地方编写该程序,其中的方法将颜色列表作为参数。颜色“红色”,“红色”,“绿色”和“绿色”应替换为“黑色”,“黑色”,“白色”和“白色”(请参见代码文本中的字典“ self.colour_replacement”)

如果列表中有红色/绿色(不管大写字母)中只有两种颜色,则程序应仅返回列表而不进行更改。

Example_1:
print(c.make_readable(['Green', 'Green', 'pink', 'green', 'yellow', 'green', 
'green']))

should return: 
['Green', 'Green', 'pink', 'green', 'yellow', 'green', 'green']


Example_2: 
print(c.make_readable(['green', 'Green']))

should return:
['green', 'Green']

我认为问题与该行中的“ or”和“ and”语句有关:

if ('red' and 'green') in colours or ('Red' and 'green') in colours or 
('red' and 'Green') in colours or ('Red' and 'Green') in colours:  

但是我不确定。

class ColourChanger:
    def __init__(self):
    """A dictionary that shows the 
    replacement colours."""
        self.colour_replacement = {'Green' : 'White', 'red': 'black', 
    'green': 'white', 'Red' : 'Black'}

    def make_readable(self, colours):
        result = []    
        if ('red' and 'green') in colours or ('Red' and 'green') in colours or 
        ('red' and 'Green') in colours or ('Red' and 'Green') in colours:
            for col in colours:
                if col in self.colour_replacement:
                    result.append(self.colour_replacement[col]) """Appends the 
                   "result"-list with the replacement color instead of the color (col)."""
                else:
                    result.append(col)

        else:
            return colours

        return result


c = ColourChanger()
print(c.make_readable(['green', 'Green']))
print(c.make_readable(['Red', 'red']))
print(c.make_readable(['Red', 'Red', 'pink', 'red', 'yellow', 'red', 'red', 
'Green']))
print(c.make_readable(['Green', 'Green', 'pink', 'green', 'yellow', 'green', 
'green']))


Expected output:
['green', 'Green']
['Red', 'red']
['Black', 'Black', 'pink', 'black', 'yellow', 'black', 'black', 'White']
['Green', 'Green', 'pink', 'green', 'yellow', 'green', 'green']

Actual Output: 
['white', 'White']
['Red', 'red']
['Black', 'Black', 'pink', 'black', 'yellow', 'black', 'black', 'White']
['White', 'White', 'pink', 'white', 'yellow', 'white', 'white']

1 个答案:

答案 0 :(得分:1)

您的情况不对,请使用

进行修复
if ('red' in colours and 'green' in colours) or ( ...)  .... : 

但不是必需的-dupe中有很多共享。


您可以一起避免大部分if。首先检查列表中是否包含某些内容,然后有条件地构建替换项以将其返回,这几乎没有什么好处。有时您可能会省略构建替换列表,如果列表巨大会很有用,但是您仍然必须将每个列表重复遍历一次才能完全“看到”。您还需要对 all 列表执行此操作,这将需要构建替代列表-将遍历thouse然后再执行一次-消耗掉为那些保存的所有“时间”不需要更换。对于简短的(ish)列表,每次都更容易简单地构建替换项:

在第一次运行数据时构建“替换”更为容易:

class ColourChanger:
    def __init__(self):
        # A dictionary that shows the replacement colours - only lowercase needed
        self.colour_replacement = {'green' : 'white', 'red': 'black'}

    def make_readable(self, colours):
        result = []

        # iterate all col's, append them if not in dict, else append the replacement
        for col in colours:
            replace = self.colour_replacement.get(col.lower(), col)

            # adjust case-ness by string-splicing with uppercase where needed
            if col[0].isupper() and replace[0].islower(): 
                replace = replace[0].upper()+replace[1:]

            result.append(replace)

        return result



c = ColourChanger()
print(c.make_readable(['green', 'Green']))
print(c.make_readable(['Red', 'red']))
print(c.make_readable(['Red', 'Red', 'pink', 'red', 'yellow', 'red', 'red', 
'Green']))
print(c.make_readable(['Green', 'Green', 'pink', 'green', 'yellow', 'green', 
'green']))

输出:

['white', 'White']
['Black', 'black']
['Black', 'Black', 'pink', 'black', 'yellow', 'black', 'black', 'White']
['White', 'White', 'pink', 'white', 'yellow', 'white', 'white']

有关为什么使用.get(key, default)的信息,请参见Why dict.get(key) instead of dict[key]?