如何在Python中编写混淆的单行代码?
是的,可以使用Lambda在Python中编写混淆的单行代码。在继续之前,让我们首先了解Python中的Lambda是什么。
Python Lambda
Lambda表达式允许定义匿名函数。Lambda函数是一个匿名函数,即没有名称的函数。让我们看看语法:
lambda arguments: expressions
关键字lambda定义了一个lambda函数。Lambda表达式包含一个或多个参数,但只能有一个表达式。
示例
让我们看一个例子:
myStr = "Thisisit!" (lambda myStr : print(myStr))(myStr)
输出
Thisisit!
使用Lambda合并Python列表中的元素
为了使用Lambda合并元素,我们将使用reduce()方法。reduce()是Python中functools模块的一部分。让我们首先学习如何安装和使用functools模块:
安装functools模块:
pip install functools
使用functools模块:
import functools
示例
以下是代码:
import functools # List myList = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U'] # Display the List print("List = " + str(myList)) # Merge items using Lambda myList[0: 3] = [functools.reduce(lambda i, j: i + j, myList[0: 3])] # Displaying the Result print("Result = " + str(myList))
输出
List = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U'] Result = ['HOW', 'A', 'R', 'E', 'Y', 'O', 'U']
现在将使用相同的functools模块在Python中编写混淆的单行代码。同时,也使用了functools模块的reduce()方法:
素数的单行混淆代码
示例
现在让我们看一个使用functools模块的素数单行混淆代码示例:
from functools import reduce # Primes < 500 print(list(filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0, map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,500)))))
输出
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499]
斐波那契数列的单行混淆代码
示例
现在让我们看一个使用functools模块的前20个斐波那契数列的单行混淆代码示例:
from functools import reduce # First 20 Fibonacci numbers print(list(map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1: f(x,f), range(20))))
输出
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
曼德勃罗集的单行混淆代码
示例
现在让我们看一个使用functools模块的曼德勃罗集的单行混淆代码示例:
from functools import reduce # Mandelbrot set print((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y, Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM, Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro, i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y >=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr( 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)) # \___ ___/ \___ ___/ | | |__ lines on screen # V V | |______ columns on screen # | | |__________ maximum of "iterations" # | |_________________ range on y axis # |____________________________ range on x axis
输出
BBBBBBBBBBBBBBBCCCCCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDCCCCCCCCCCCCCCCBBBBBBBBBBBBBCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDEEEEEEFGJJHFFFEEEEEDDDDDCCCCCCCCCCBBBBBBBBBBBCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFGHJPJKL_FEEEEEEDDDDDDDCCCCCBBBBBBBBBCCCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFFGHIJZR_QJIGFFFEEEEEEDDDDDDDCCBBBBBBBBCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFGGGHIKP______SKHGFFFFFEEEEDDDDDDDBBBBBBBCCCDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFHHIXIIIIJKLO______NKJJIHGGGHSGFEEDDDDDBBBBBBCCDDDDDDDDDDDDDDDDDDDEEEEEFFFFFFFGGHIM___R_________________PLN[SQXIFEEDDDDBBBBBCDDDDDDDDDDDDDDDDEEEFFFFFFFFFFFGGGHIKMNS_________________________UIGGFEEDDDBBBBBDDDDDDDDDDEEEEEFFGHOIHHHHHHHHHHHHIJLP____________________________RKJPGFEEDDBBBBDDDDEEEEEEEEEFFFFGGHJM___LNW_ONKJJKNY______________________________SKHGFEEEDBBBBDEEEEEEEEEFFFFFFGHHJLOT__________QPZ_________________________________HFFEEEEBBBDEEEEEEEFGGGGHHHJM_QNS______________________________________________[HGFFEEEEBBB_________________________________________________________________QLIHGFFFEEEEBBBDEEEEEEEFGGGGHHHJM_QNS______________________________________________[HGFFEEEEBBBBDEEEEEEEEEFFFFFFGHHJLOT__________QPZ_________________________________HFFEEEEBBBBDDDDEEEEEEEEEFFFFGGHJM___LNW_ONKJJKNY______________________________SKHGFEEEDBBBBBDDDDDDDDDDEEEEEFFGHOIHHHHHHHHHHHHIJLP____________________________RKJPGFEEDDBBBBBCDDDDDDDDDDDDDDDDEEEFFFFFFFFFFFGGGHIKMNS_________________________UIGGFEEDDDBBBBBBCCDDDDDDDDDDDDDDDDDDDEEEEEFFFFFFFGGHIM___R_________________PLN[SQXIFEEDDDDBBBBBBBCCCDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFHHIXIIIIJKLO______NKJJIHGGGHSGFEEDDDDDBBBBBBBBCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFGGGHIKP______SKHGFFFFFEEEEDDDDDDDBBBBBBBBBCCCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFFGHIJZR_QJIGFFFEEEEEEDDDDDDDCCBBBBBBBBBBBCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFGHJPJKL_FEEEEEEDDDDDDDCCCCCBBBBBBBBBBBBBCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDEEEEEEFGJJHFFFEEEEEDDDDDCCCCCCCCCC
广告