繁体   English   中英

如何将其转换为Python代码?

[英]How do I translate this into Python code?

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代码? 我实际上是在处理熊猫数据框,我只是以列表为例对其进行了简化。

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

使用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 {}" ...

Python 2一线:

[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)]

另一个Python 2单行代码。 这个处理相等的项目。

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

可以通过将cmp定义为python 3来运行:

cmp = lambda x,y : 0 if x==y else -1 if x < y else 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]))
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]))
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

使用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

使用递归:

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)

暂无
暂无

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

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