我如何将其转换为Python代码?

时间:2017-06-03 12:22:24

标签: python loops

x = [1, 3, 2, 5, 7]

从列表的第一个值开始:

如果下一个值更大,则会打印"\nthe value x is greater than y"

如果下一个值相等,则会打印"\nthe value x is equal to y"

如果下一个值较小,则会打印"\nthe value x is smaller than y"

如何将其转换为精确的Python代码?我实际上使用的是pandas数据框,我只是以列表为例进行了简化。

x = [1, 3, 2, 5, 7]

根据上面给出的,输出应该是这样的:

the value 3 is greater than 1
the value 2 is smaller than 3
the value 5 is greater than 2
the value 7 is greater than 5

8 个答案:

答案 0 :(得分:6)

使用str.join直接生成输出,并使用自身的移位版本压缩列表,以便在理解中进行比较:

x = [1, 3, 2, 5, 7]

output = "\n".join(["the value {} is {} than {}".format(b,"greater" if b > a else "smaller",a) for a,b in zip(x,x[1:])])

print(output)

(请注意"大于"或"小于"不严格并适用于相等的值,即使它令人困惑,所以可能是第三种选择如果案件可能发生,可以创建处理这些案件,如本案所述)

结果:

the value 3 is greater than 1
the value 2 is smaller than 3
the value 5 is greater than 2
the value 7 is greater than 5

您可以使用这些变体来摆弄换行符:

"".join(["the value {} is {} than {}\n" ...

"".join(["\nthe value {} is {} than {}" ...

答案 1 :(得分:4)

Python 2 one liner:

[print(str(l[i+1])+" is greater than" + str(l[i])) if l[i+1]>l[i] else print(str(l[i+1])+" is smaller than" + str(l[i])) for i in range(len(l)-1)]

答案 2 :(得分:2)

另一个Python 2单线程。这个处理相同的项目。

git rev-list --ancestry-path 7b4a07a..ecf5891

<强>输出

x = [1, 3, 2, 5, 5, 7]
print '\n'.join('the value %s is %s %s'%(u,['equal to','greater than','less than'][cmp(u,v)],v)for u,v in zip(x[1:],x))

通过将the value 3 is greater than 1 the value 2 is less than 3 the value 5 is greater than 2 the value 5 is equal to 5 the value 7 is greater than 5 定义为:

,可以使用python 3进行运行
cmp

答案 3 :(得分:1)

可以使用for循环和三元运算符,如下所示;

x = [1, 3, 2, 5, 7]
for i in range(len(x)-1):
    comparison = "greater than" if x[i+1]>x[i] else ("equal to" if x[i+1]==x[i] else "less than")
    print("The value {0} is {1} {2}.".format(x[i+1],comparison,x[i]))

答案 4 :(得分:1)

def cmp(item1, item2):
    if item2 == item1:
        return "{} is equal to {}".format(item2, item1)
    elif item2 >= item1:
        return "{} is greater than {}".format(item2, item1)
    elif item2 <= item1:
        return "{} is less than {}".format(item2, item1)
    else:
        return "Invalid item(s)."


x = [1, 3, 2, 5, 7]

for i in range(len(x)-1):
    print(cmp(x[i],x[i+1]))

答案 5 :(得分:1)

x = [1,4,5,3,4]

for i in range(0, len(x) - 1):
    out = "is equal to"
    if (x[i] < x[i + 1]):
        out = "is greater than"
    elif (x[i] > x[i + 1]):
        out = "is less than"

    print ("%s %s %s" % (x[i + 1], out, x[i]))

您还需要解释吗?

编辑: 糟糕,它会输出:
4大于1
5大于4
3小于5
4大于3

答案 6 :(得分:1)

使用lambda函数的示例

x = [1, 3, 2, 5, 7]

greater = lambda a, b: a > b

old_i = x[0]
for i in x[1::]:
    if old_i :
        print(i,"is", 
              "greater" if greater(i, old_i) else "smaller","than",old_i)
    old_i = i

输出

3 is greater than 1
2 is smaller than 3
5 is greater than 2
7 is greater than 5

答案 7 :(得分:1)

使用递归:

def foo(n, remaining):
  if not remaining:
    return
  if n < remaining[0]:
    print('the value {} is greater than {}'.format(remaining[0], n))
  else:
    print('the value {} is smaller than {}'.format(remaining[0], n))
  foo(remaining[0], remaining[1:])


def the_driver(num_list):
  foo(num_list[0], num_list[1:])


if __name__ == '___main__':
  x = [1, 3, 2, 5, 7]
  the_driver(x)