【问题标题】:Passing set, list and Dictionary as argument to Python function将集合、列表和字典作为参数传递给 Python 函数
【发布时间】:2016-07-22 18:23:27
【问题描述】:

请检查我下面的代码并进行必要的更正。它给了我一条错误消息。

作业如下所示。

创建一个函数manipulate_data 执行以下操作

  1. 接受string作为第一个参数,指定要使用的数据结构list、set或dictionary

  2. 根据指定的数据结构接受要操作的数据作为第二个参数,例如 [1, 4, 9, 16, 25] 用于 list 数据结构

  3. 基于第一个参数

    • 返回列表的反向或
    • 将项目"ANDELA"、"TIA" 和"AFRICA" 添加到集合并返回结果集合
    • 返回字典的键。

我的解决方案代码如下:

def manipulate_data(argument1, argument2):
   if argument1 == "list":
     argument2.reverse()
return argument2

if argument1 == "set":
   argument2.add("ANDELA")
   argument2.add("TIA")
   argument2.add("AFRICA")
return argument2

if argument1 == "dictionary":
   argument2.keys()
return argument2

我不知道我是否说得通。收到的错误信息是:

Total Specs: 3 Total Failures: 2

test_manipulate_dictionary
Failure in line 23, in test_manipulate_dictionary self.assertEqual(result, ["grapes", "mangoes", "apples", "oranges"], msg = "Dictionary not manipulated correctly") AssertionError: Dictionary not manipulated correctly

和

test_manipulate_set
    Failure in line 19, in test_manipulate_set self.assertEqual(result, {"a", "b", "c", "d", "e", "ANDELA", "TIA", "AFRICA"}, msg = "Set not manipulated correctly") AssertionError: Set not manipulated correctly

【问题讨论】:

标签: python list dictionary set


【解决方案1】:
def manipulate_data(arg1, arg2):
    if arg1 == "list":
        arg2.reverse()
        return arg2,type(arg2)

    #http://stackoverflow.com/questions/15768757/how-to-construct-a-set-out-of-list-items
    if arg1 == "set":
        myset=set(arg2)
        myset.add("ANDELA")
        myset.add("TIA")
        myset.add("AFRICA")
        return myset,type(myset)

    if arg1 == "dictionary":
        mydict={}
        for l in arg2:
            mydict[l]=l
        return mydict.keys(),type(mydict)


print manipulate_data("list",[1, 4, 9, 16, 25])
print manipulate_data("set",[1, 4, 9, 16, 25])
print manipulate_data("dictionary",[1, 4, 9, 16, 25])

输出:

([25, 16, 9, 4, 1], <type 'list'>)
(set([1, 4, 9, 'AFRICA', 16, 25, 'TIA', 'ANDELA']), <type 'set'>)
([16, 1, 4, 25, 9], <type 'dict'>)

【讨论】:

    【解决方案2】:

    您在正确的轨道上,但我认为您在 dict 和 set 的情况下错误地将参数传递给您的函数,因为默认情况下 {} 表示字典

    类似这样的:

    def manipulate_data(argument1, argument2):
        if argument1 == "list":
            argument2.reverse()
            return argument2
    
        if argument1 == "set":
            argument2.update(["ANDELA", "TIA", "AFRICA"])
            return argument2
    
        if argument1 == "dictionary":
            return argument2.keys()
    
    print(manipulate_data('list', [1,2,3,4,5,6]), manipulate_data('set', {1,2,3,4,5,6}), manipulate_data('dictionary', {1:2,3:4,5:6}), sep='\n')
    

    输出:

    $ python3 a.py 
    [6, 5, 4, 3, 2, 1]
    {'ANDELA', 1, 2, 3, 4, 5, 6, 'TIA', 'AFRICA'}
    dict_keys([1, 3, 5])
    $
    

    【讨论】:

      【解决方案3】:
      def manipulate_data(argument1, argument2):
          if argument1 == "list":
              if isinstance(argument2, list):
                  argument2.reverse()
                  return argument2
      
          if argument1 == "set":
              if isinstance(argument2, set):
                  argument2.add("ANDELA")
                  argument2.add("TIA")
                  argument2.add("AFRICA")
                  return argument2
      
          if argument1 == "dictionary":
              if isinstance(argument2, dict):
                  return argument2.keys()
      
      l = [1,2,3]
      d = {'a':1, 'b':2, 'c':3 }
      s = set()
      s.add(1)
      s.add(2)
      s.add(3)
      
      print manipulate_data("list", l)
      print manipulate_data("dictionary", d)
      print manipulate_data("set", s)
      

      【讨论】:

      • 如果您能在回答中解释 OP 的代码有什么问题以及您为解决问题所做的更改,将会很有帮助。
      • 我已经在为他/她做功课了,伙计。坦率地说,代码很容易解释。
      • 如果代码不言自明,应该很容易解释。如果您认为该问题不适合正确回答,请将其标记为已关闭。答案有一定的责任。
      • 感谢 marienbad。你的代码已经回答了我的问题。
      猜你喜欢
      • 2017-10-01
      • 2023-03-26
      • 2013-10-29
      • 2010-09-24
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多