【问题标题】:Choose list which is returned by def选择 def 返回的列表
【发布时间】:2013-03-12 13:55:06
【问题描述】:

我有一个定义来分隔特定属性的一些坐标。 对于这种分离,我使用 1 个定义,在定义中我有 9 个列表(不同的标准)。现在对于输出,我只想要我定义的列表。否则我不能用它来绘图。

def sorteerCord(cord):
    tweestijging=[]
    stijginggelijk=[]
    stijgingdaling=[]
    tweedaling=[]
    dalinggelijk=[]
    dalingstijging=[]
    tweegelijk=[]
    gelijkstijging=[]
    gelijkdaling=[]

    y=0
    while y<len(cord):
        lijst=cord[y]
        if (lijst[1]-lijst[0])>0.5:
            if (lijst[2]-lijst[1])>0.5:
                tweestijging.append(y)
            if (lijst[2]-lijst[1])<=0.5 and (lijst[2]-lijst[1])>=-0.5:
                stijginggelijk.append(y)
            if (lijst[2]-lijst[1])<-0.5:
                stijgingdaling.append(y)

        if (lijst[1]-lijst[0])<-0.5:
            if (lijst[2]-lijst[1])>0.5:
                dalingstijging.append(y)
            if (lijst[2]-lijst[1])<=0.5 and (lijst[2]-lijst[1])>=-0.5:
                dalinggelijk.append(y)
            if (lijst[2]-lijst[1])<-0.5:
                tweedaling.append(y)

        if (lijst[1]-lijst[0])<=0.5 and (lijst[1]-lijst[0])>=-0.5:
            if (lijst[2]-lijst[1])>0.5:
                gelijkstijging.append(y)
            if (lijst[2]-lijst[1])<=0.5 and (lijst[2]-lijst[1])>=-0.5:
                tweegelijk.append(y)
            if (lijst[2]-lijst[1])<-0.5:
                gelijkdaling.append(y)

        y=y+1       
    print raw_input()
    return raw_input()

他们是在我的 def 中定义输出文件的一种方式吗 (def sorteerdCord(cord,outpu=tweestijging)

【问题讨论】:

  • “输出文件是什么样的”是什么意思?它是什么样的?
  • 根据谷歌翻译,我发现这是荷兰语。但是,如果您将变量名称更改为在英语中更有意义,这对所有人都会有用,因为像我这样不会说/不懂荷兰语的人必须查找名称。

标签: python user-defined-functions


【解决方案1】:

我猜在最后两行中,您希望用户输入要使用的输出列表,但不太确定。您可以使用字典将输入字符串映射到变量。

类似:

def sorteerCord(cord, output):
    # all of your separation code
    outputmap = { 'tweestijging': tweestijging,
                  'gelijkstijging' : gelijkstijging,
                   # and more of those
                 }
    return outputmap[ output ]

然后调用:

sorteerCord(cord, 'gelijkstijging')

您当然也可以选择返回所有列表或将它们保存在字典中:

output = { 'tweestijging': [],
       'gelijkstijging': [],
        # etc
        }

 # code to manipulate lists goes here

return output

然后使用相同的技术选择一个。

【讨论】:

  • 我犯了一个错误,将整个列表声明列表转换为 dict 文字,让您有足够的时间在我面前回答! :-)
  • 我想知道为什么我是第一个。 SO 的代码竞赛是恶性的。
  • 谢谢你,这正是我的意思。我很抱歉我的英语解释不好,但这是我需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 2017-08-24
相关资源
最近更新 更多