简体   繁体   English

Python:将函数调用的列表索引作为它自己的参数传递

[英]Python: pass a function call's list index as its own argument

I've got a rather sizable (3.9 kB) script that's designed to align some text based on some parameters, centering, etc. Here are the blocks I'm looking to improve: (Apologies for the somewhat-codegolf but the point is not what the functions do but their structure: they work fine as it is in theory but I ache to make them shorter)我有一个相当大的 (3.9 kB) 脚本,旨在根据某些参数、居中等对齐一些文本。以下是我希望改进的块:(为有点代码高尔夫道歉,但重点不是这些函数除了它们的结构之外还能做什么:它们在理论上可以正常工作,但我想让它们变短)

    #manage + control the margin in spaces between the body text and right vertical rule
def calcMgn(lnNum): return toEven(bwinner-(len(LnOpn[lnNum])+len(LnCtn[lnNum])),-1)//2
def calcRMgn(lnNum): return (0-(1-(toEven(((bwinner-(len(LnOpn[lnNum])+len(LnCtn[lnNum])))//2),-1))))
def calcLenOf(lnNum): return len(LnMgn[lnNum])+len(LnOpn[lnNum])+len(LnCtn[lnNum])+calcRMgn(lnNum)
def calcRDiff(lnNum): return LnMgnR[lnNum] - (lenOf[lnNum] - bwinner)
def calcRMgnSpa(lnNum): return ((LnMgnRAdjust[lnNum])-adjust)
    #there absolutely must be a better way to call a function based on its position in a list than the following:
LnMgn=[calcMgn(0)*spa,calcMgn(1)*spa,calcMgn(2)*spa,calcMgn(3)*spa]
LnMgnR=[calcRMgn(0),calcRMgn(1),calcRMgn(2),calcRMgn(3)]
lenOf=[calcLenOf(0),calcLenOf(1),calcLenOf(2),calcLenOf(3)]
LnMgnRAdjust=[calcRDiff(0),calcRDiff(1),calcRDiff(2),calcRDiff(3)]
LnMgnR_spa=[calcRMgnSpa(0)*spa,calcRMgnSpa(1)*spa,calcRMgnSpa(2)*spa,calcRMgnSpa(3)*spa,]
    #take the lengths for a test drive to see if they break any rules
testLen=[LnMgn[0] + LnOpn[0] + spa + LnCtn[0] + LnMgnR_spa[0],\
LnMgn[1] + LnOpn[1] + spa + LnCtn[1] + LnMgnR_spa[1],\
LnMgn[2] + LnOpn[2] + spa + LnCtn[2] + LnMgnR_spa[2],\
LnMgn[3] + LnOpn[3] + spa + LnCtn[3] + LnMgnR_spa[3]] #instead of this, I want something like a for statement or ???
for i in range(0,3):
    if len(testLen[i]) > bwinner:
        LnMgnR_spa[i] = int((toEven(LnMgnRAdjust[i])-adjust)-(len(testLen[i])-bwinner))*str(spa)
    #concatenate strings
addLine=[idt + vl + LnMgn[0] + LnOpn[0] + spa + LnCtn[0] + LnMgnR_spa[0] + vr + nl,\
idt + vl + LnMgn[1] + LnOpn[1] + spa + LnCtn[1] + LnMgnR_spa[1] + vr + nl,\
idt + vl + LnMgn[2] + LnOpn[2] + spa + LnCtn[2] + LnMgnR_spa[2] + vr + nl,\
idt + vl + LnMgn[3] + LnOpn[3] + spa + LnCtn[3] + LnMgnR_spa[3] + vr + nl]



I know SO's policy of "we won't write your code for you" and I definitely don't intend to ask for such favours, just guidance: is there a way (and I have googled, and googled) to make LnMgn and its similar arrays more efficient and less huge by its contents dynamically iteravely calling and self-defining based on their array index?我知道 SO 的“我们不会为你编写代码”的政策,我绝对不打算要求这样的帮助,只是指导:有没有办法(我已经用谷歌搜索过)来制作 LnMgn 及其类似的数组通过其内容动态迭代调用和基于其数组索引的自定义而更高效且体积更小?

You can also create a list of functions:您还可以创建函数列表:

func_list = [f1, f2, f3, f4]

and call them, for example, like:并调用它们,例如:

[func_list[i](i) for i in range(4)]

This example would return:此示例将返回:

[f1(0), f2(1), f3(2), f4(3)]

Just use a list comprehension.只需使用列表理解。 One example:一个例子:

LnMgn = [calcMgn(index)*spa for index in range(4)]

You can do the same for all the others.你可以对所有其他人做同样的事情。

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

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