在Python中打印真实组的更好方法

时间:2017-02-27 16:15:51

标签: python

我想制作一个程序,它返回我在程序中找到的一组True变量。像这样:

1 = True
2 = True
3 = False
4 = False
5 = True

我想要的是作为印刷品返回

The true numbers are: 1, 2 and 5

编辑1:

代码是一个字母计数器!

组中的eacha字母有值。

像 a = 1时 b = 2 ...

如果一个数字重复超过4次,那么该数字是真的

这个小组将是一个名字。就像约翰一样。

程序读取它并为每个字母提供一个数字。

我现在正在使用的是这个(Preatty丑陋,我知道,但我本月开始编程......),其中" a"是名称中字母a的数量,b是名称中的b数量....

if (a + j + s) >=4:
    exe1 = 1
else:
    exe1 = ""

if (b + k + t) >=4:
    exe2 = 2
else:
    exe2 = ""

if (c + l + u) >=4:
    exe3 = 3
else:
    exe3 = ""

if (d + m + v) >=4:
    exe4 = 4
else:
    exe4 = ""

if (e + n + w) >=4:
    exe5 = 5
else:
    exe5 = ""

if (f + o + x) >=4:
    exe6 = 6
else:
    exe6 = ""

if (g + p + y) >=4:
    exe7 = 7
else:
    exe7 = ""

if (h + q + z) >=4:
    exe8 = 8
else:
    exe8 = ""

if (i + r) >=4:
    exe9 = 9
else:
    exe9 = ""


print("Excesses:", exe1, exe2, exe3, exe4, exe5, exe6, exe7, exe8, exe9)

1 个答案:

答案 0 :(得分:0)

你为什么不用字典?以此代码为例。

- (void)testViewControllerHasNoRetainCycles
{
    UIViewController *viewController = [[UIViewController alloc] init];

    // Trigger `veiwDidLoad`
    // commenting out this line passes the test
    [viewController view];

    // Make a weak ref and set the original ref to nil
    // This should mean the weak ref also becomes nil
    // since there is nothing left retaining it
    // (unless there is a retain cycle)
    __weak UIViewController * weakViewController = viewController;
    viewController = nil;

    // This fails
    XCTAssertNil(weakViewController);
}

现在你的列表 - 虽然在括号内,我不知道如何删除它们,因为它们是列表的一部分...也许迭代它应该解决问题,虽然我不是一个真正的Python专家