简体   繁体   English

打印出python中变量之间的关系列表?

[英]print out list of the relation between variables in python?

I'm an absolute beginner with python and my professor is sort of doing a trial of fire approach to teaching us the language. 我绝对是python的初学者,而我的教授正在尝试使用fire方法来教我们该语言。 The goal is to list the triples in the relation {(a,b,c) : a, b, and c are integers with 0 < a < b < c < 5}. 目的是列出关系{(a,b,c)中的三元组:a,b和c是0 <a <b <c <5}的整数。 Use range(5) as the source for values of a, b, and c. 使用range(5)作为a,b和c值的来源。

I think i understand the general concept of what it is, i set the values of a,b,c fine, but I don't quite get how I can print out variables like that? 我想我了解它的一般概念,我将a,b,c的值设置得很好,但是我不太了解如何打印出这样的变量? I was thinking of using some sort of while loop of like while a < b < c print out (a,b,c) or something but I can't get it to work with the syntax. 我当时想使用诸如while <b <c打印输出(a,b,c)之类的某种while循环,但我无法使其与语法一起使用。

Thanks so much for the help! 非常感谢你的帮助!

So you need to vary each of a , b , and c in the range 0-4 (which is what range(5) produces). 因此,您需要在0-4范围内改变abc的范围(这是range(5)产生的值)。 For this you can use three nested for loops. 为此,您可以使用三个嵌套的for循环。

for a in range(5):
    for b in range(5):
        for c in range(5):

This will ensure that for each possible value of a , all possible values of b are tried, and similarly, for each possible value of b , all possible values of c are tried. 这将确保对的每个可能值a ,的所有可能值b被尝试,并且类似地,对于每个可能的值b ,的所有可能值c被尝试。 In other words, the innermost (bottom-most, as they're written) loop c will go through 0-4 for each value of b , which is also going through 0-4 for each value of a , which is also going through 0-4. 换句话说,最内层(最底层,因为他们写的)环c会经过0-4的每个值b ,这也是通过0-4打算为每个值a ,这也是通过去0-4。

Now inside these loops, you just need to check to see if the criterion your prof has given is true, and if so, print the numbers. 现在,在这些循环中,您只需要检查教授给出的条件是否为真,如果是,则打印数字。 Fortunately, the syntax your prof has given works fine in Python: 幸运的是,您的教授给出的语法在Python中可以正常工作:

if 0 < a < b < c < 5:
    print(a, b, c)

Putting it all together, then, we have: 综上所述,我们有:

for a in range(5):
    for b in range(5):
        for c in range(5):
            if 0 < a < b < c < 5:
                print(a, b, c)

Now, this works fine, and will undoubtedly satisfy your professor, but there are some simple improvements you can make to this program. 现在,这可以很好地工作,并且无疑会令您的教授满意,但是您可以对该程序进行一些简单的改进。 First, the variables each go from 0-4, but the value 0 is never going to pass the if test, because it will never be less than 0! 首先,每个变量都从0-4开始,但是值0永远不会通过if测试,因为它永远不会小于0! So why even try it? 那么,为什么还要尝试呢? You could save a bit of work by writing each range as range(1, 5) . 通过将每个范围写为range(1, 5)可以节省一些工作。

But... there's also the fact that if a is 1, b can never be 1 (because a < b must be true, and if a is 2, b can likewise never be 2, and so on. And also, from the other end of the range, if c is 4, b can never be 4 and satisfy the test. We can write the ranges accordingly, so that the range for b starts 1 past a and leaves off one before c , so that those combinations of values need never be tested. 但是...还有一个事实是,如果a为1,则b永远不会为1(因为a < b必须为true,并且如果a为2, b同样也不会为2,依此类推。依此类推。该范围的另一端,如果c是4, b永远不能4和满足测试,我们可相应地写范围,从而对于范围b开始1过去a和叶关闭一个 c ,从而使那些组合值无需测试。

Finally, your if statement can also be simplified: because of the ranges being used, you don't need to make sure 0 is less than a ; 最后,您的if语句也可以简化:由于使用了范围,因此无需确保0小于a that will always be true because a starts at 1. At the same time, you know that c will always be less than 5 because the range() makes it that way, so you can remove that test too. 因为a从1开始,所以它将始终为真。同时,您知道c总是小于5,因为range()这样做,所以也可以删除该测试。

So you will get identical results, while doing less work, by writing it like this: 因此,通过这样编写,您将在减少工作量的同时获得相同的结果:

for a in range(1, 3):
    for b in range(a+1, 4):
        for c in range(b+1, 5):
            if a < b < c:
                print(a, b, c)

In this case, the extra work is not that important... both versions are so fast you will never notice the extra time the first approach took. 在这种情况下,额外的工作并不重要……两个版本都非常快,您永远都不会注意到第一种方法所花费的额外时间。 But it's always worth thinking about how a problem can be rethought to do less work; 但是,始终值得考虑如何重新思考问题以减少工作量; sometimes it's the difference between a usable program and an unusable one. 有时是可用程序和不可用程序之间的区别。

In this case, the initial version goes through 125 tests. 在这种情况下,初始版本将经过125次测试。 The second, more efficient version? 第二个更有效的版本? Four. 四。 Which happens to be exactly the same as the number of correct results! 恰好与正确结果的数量完全相同!

So we can take out the if statement entirely, and just print every combination, because the loops we've written make sure that only numbers that pass the test are used to begin with. 因此,我们可以完全取出if语句,并只打印每个组合,因为我们编写的循环确保仅使用通过测试的数字作为开头。

And we've done less than 1/30th of the work in the process! 在此过程中,我们完成的工作还不到三分之一。

How about this? 这个怎么样?

for a in range(5):
    for b in range(5):
        for c in range(5):
            if 0 < a < b < c < 5:
                print(a, b, c)

This will iterate over the values 0 till 4 for a , b and c and it'll print them when the condition 0 < a < b < c < 5 holds. 这将迭代abc的值04 ,并且当条件0 < a < b < c < 5成立时,将打印它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM