【问题标题】:Python append method not working correctly [closed]Python追加方法无法正常工作[关闭]
【发布时间】:2014-06-13 12:37:35
【问题描述】:
def sem1Sort1(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 1:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def sem1Sort2(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if semester1 == 2:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def main():

    selectionSEM1 = ["a", "b", "c", "d", "e", "f", "g", "h"]
    selectionSEM2 = []

    semester1 = {

    1: ["e", "f", "g", "h"], 2: ["a", "b", "c", "d"] ,

    }

    SEM1period1 = sem1Sort1(semester1, selectionSEM1)
    SEM1period2 = sem1Sort2(semester1, selectionSEM1)

    print SEM1period1
    print SEM1period2

main()

当我运行此代码时,它可以很好地打印出 SEM1period1,如 ["e"、"f"、"g"、"h"],但第二种方法 sem1Sort2 似乎没有将任何内容保存到 SEM1period2 中 - 作为打印语句打印出 []

更新:

def sem1Sort1(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 1:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def sem1Sort2(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 2:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def sem1Sort3(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 3:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

def main():

    selectionSEM1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]
    selectionSEM2 = []


    semester1 = {
    1: ["e", "f", "g", "h"], 2: ["a", "b", "c", "d"] ,
    3: ["i", "j", "k", "l"]
    }

    SEM1period1 = sem1Sort1(semester1, selectionSEM1)
    SEM1period2 = sem1Sort2(semester1, selectionSEM1)
    SEM1period3 = sem1Sort3(semester1, selectionSEM1)

    print SEM1period1
    print SEM1period2
    print SEM1period3

main()

为什么 print SEM1period3 没有返回?

【问题讨论】:

  • 修正了初出茅庐指出的错误后,sem1Sort1sem1Sort2 的功能会完全一样,那为什么会有两个不同的功能呢?而且list在python中是一个type,所以创建一个名为list的变量也不是很明智。
  • 伙计们,你不需要投票给我......我只是一个新手,正在寻找一些答案。对不起。
  • 回答完毕后请不要再添加问题。
  • 你没有任何返回值,所以在 sem1Sort3 方法中使用“返回列表”...以后先跟踪你自己,然后你可以提问。
  • 新问题应该作为新问题发布。发布时请随意参考。

标签: python arrays list methods dictionary


【解决方案1】:

这里您将semester1 与整数进行比较,但semester1sem1Sort2 函数中的dict 对象,

    for period in semester1:
        if semester1 == 2:

实际上你必须像这样将整数与dict的键进行比较,

    for period in semester1:
        if period == 2:

而你剩下的就是这样,

    def sem1Sort1(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 1:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def sem1Sort2(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 2:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def main():

    selectionSEM1 = ["a", "b", "c", "d", "e", "f", "g", "h"]
    selectionSEM2 = []

    semester1 = {

    1: ["e", "f", "g", "h"], 2: ["a", "b", "c", "d"] ,

    }

    SEM1period1 = sem1Sort1(semester1, selectionSEM1)
    SEM1period2 = sem1Sort2(semester1, selectionSEM1)

    print SEM1period1
    print SEM1period2

main()

输出:

['e', 'f', 'g', 'h']
['a', 'b', 'c', 'd']

【讨论】:

  • 现在都没有返回任何东西...?
  • 对不起,我有点新手,你必须给我更多的方向,对不起。
  • @LucasGrillos 请查看更新后的答案。
  • 其实for period in semester1:for period in semester1.keys(): 做同样的事情。
  • 非常感谢您的帮助!
【解决方案2】:

为什么这么复杂?

您不需要遍历所有 dict 条目并挑选出匹配的条目 - 这使得整个 dict 的使用毫无意义。

相反,您可以告诉 dict 为您提供与给定键关联的值。您可以在main() 函数中这样做,将函数简化为

def semSort(semester, selection):
    list = []
    for index in semester:
        if index in selection:
            list.append(index)
    return list

你可以在main()函数中调用比如

def main():
    selectionSEM1 = ["a", "b", "c", "d", "e", "f", "g", "h"]
    selectionSEM2 = []
    semester1 = {
        1: ["e", "f", "g", "h"], 2: ["a", "b", "c", "d"] ,
    }

    SEM1period1 = semSort(semester1[1], selectionSEM1)
    SEM1period2 = semSort(semester1[2], selectionSEM1)

    print SEM1period1
    print SEM1period2

main()

你会得到你想要的。

你甚至可以改进它:

def semSort(semester, selection):

    result = []
    sel_set = set(selection)    
    for index in semester:
        if index in sel_set:
            result.append(index)

    return result

该集合使查找速度更快。

def semSort(semester, selection):
    sel_set = set(selection)
    return [index for index in semester if index in sel_set]

更加紧凑。

【讨论】:

    【解决方案3】:
    def sem1Sort1(semester1, selectionSEM1):
     list = []
     for period in semester1:
       if period == 1:
         for index in semester1[period]:
           if index in selectionSEM1:
              list.append(index)
    
     return list
    
    def sem1Sort2(semester1, selectionSEM1):
     list = []
     for period in semester1:
      if period == 2:
       for index in semester1[period]:
        if index in selectionSEM1:
           list.append(index)
     return list
    
    def main():
     selectionSEM1 = ["a", "b", "c", "d", "e", "f", "g", "h"]
     selectionSEM2 = []
     semester1 = {
     1: ["e", "f", "g", "h"], 2: ["a", "b", "c", "d"] ,
     }
     SEM1period1 = sem1Sort1(semester1, selectionSEM1)
     SEM1period2 = sem1Sort2(semester1, selectionSEM1)
     print SEM1period1
     print SEM1period2
    main()
    

    【讨论】:

    • 你写的像(如果学期 == 2:)这需要总字典值,所以我们要提及特定的键值
    • 为了更好地回答这个问题,您可以指出差异并详细说明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多