试图了解在python创建变量或使用表达式中哪个更好 [英] Trying to understand which is better in python creating variables or using expressions

查看:116
本文介绍了试图了解在python创建变量或使用表达式中哪个更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一开始就在Python中采用的一种做法是,与尝试在SAS或Fortran中执行相同操作时创建的变量数量相比,减少创建的变量数量。

One of the practices I have gotten into in Python from the beginning is to reduce the number of variables I create as compared to the number I would create when trying to do the same thing in SAS or Fortran

例如,这是我今晚编写的一些代码:

for example here is some code I wrote tonight:

def idMissingFilings(dEFilings,indexFilings):
    inBoth=set(indexFilings.keys()).intersection(dEFilings.keys())
    missingFromDE=[]
    for each in inBoth:
        if len(dEFilings[each])<len(indexFilings[each]):

            dEtemp=[]
            for filing in dEFilings[each]:
                #dateText=filing.split("\\")[-1].split('-')[0]
                #year=dateText[0:5]
                #month=dateText[5:7]
                #day=dateText[7:]
                #dETemp.append(year+"-"+month+"-"+day+"-"+filing[-2:])    
            dEtemp.append(filing.split('\\')[-1].split('-')[0][1:5]+"-"+filing.split('\\')[-1].split('-')[0][5:7]+"-"+filing.split('\\')[-1].split('-')[0][7:]+"-"+filing[-2:])
            indexTemp=[]
            for infiling in indexFilings[each]:
                indexTemp.append(infiling.split('|')[3]+"-"+infiling[-6:-4])
            tempMissing=set(indexTemp).difference(dEtemp)
            for infiling in indexFilings[each]:
                if infiling.split('|')[3]+"-"+infiling[-6:-4] in tempMissing:
                    missingFromDE.append(infiling)
    return missingFromDE

现在我在dEtemp.append(blah blah blah)行中将要处理的字符串之一拆分了4次

Now I split one of the strings I am processing 4 times in the line dEtemp.append(blah blah blah)

filing.split('\\')

从历史上讲,如果我要尝试在Fortran或SAS中进行同样的操作,我将对字符串进行切片,然后为要在此表达式中使用的字符串的每个部分分配一个变量。

Historically in Fortran or SAS if I were to attempt the same I would have 'sliced' my string once and assigned a variable to each part of the string that I was going to use in this expression.

我一直在强迫自己使用表达式,而不是首先解析为一个值并使用该值。我这样做的唯一原因是我在模仿别人的代码来学习,但是一直想问这个问题-我在哪里可以找到关于为什么一个人比另一个人更好的有力的讨论

I am constantly forcing myself to use expressions instead of first resolving to a value and using the value. The only reason I do this is that I am learning by mimicking other people's code but it has been in the back of my mind to ask this question - where can I find a cogent discussion of why one is better than the other

该代码将驱动器上的一组文档与这些文档的源列表进行比较,并检查驱动器中是否代表了所有来自源的文件

The code compares a set of documents on a drive and a source list of those documents and checks to see whether all of those from the source are represented on the drive

好的,被注释的部分更容易阅读,而且我决定如何回答nosklos答案

Okay the commented section is much easier to read and how I decided to respond to nosklos answer

推荐答案

<是的,最好将所有内容都放入表达式中。请使用变量。

Yeah, it is not better to put everything in the expression. Please use variables.

使用变量不仅更好,因为您只会执行一次操作并保存多次使用的值。主要原因是这种方式使代码更具可读性。如果您将变量命名为right,则它还可以用作免费的隐式文档!

Using variables is not only better because you will do the operation only once and save the value for multiple uses. The main reason is that code becomes more readable that way. If you name the variable right, it doubles as free implicit documentation!

这篇关于试图了解在python创建变量或使用表达式中哪个更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆