简体   繁体   English

如何读取作为参数传递给调用 function 的列表中的项目?

[英]How to read an item in list which is passed as an argument to a calling function?

I am trying to solve a problem in Hacker Rank and stuck below:我正在尝试解决 Hacker Rank 中的一个问题并卡在下面:

When I am trying to insert values to the list it is working fine, but when I am trying remove an item from the list, even though the value is available in list the code in the if block (if str(value) in list1:) is not getting executed.当我尝试将值插入列表时它工作正常,但是当我尝试从列表中删除一个项目时,即使该值在列表中可用,if 块中的代码(if str(value) in list1:)没有被执行。

I know I am doing some silly mistakes like passing list in a list.我知道我犯了一些愚蠢的错误,比如在列表中传递列表。 but when I try the same code in just a hardcoded list it is working perfectly fine.但是当我在一个硬编码列表中尝试相同的代码时,它工作得很好。

Example: Sample input:示例:示例输入:

12  
insert 0 5  
insert 1 10  
insert 0 6  
print  
remove 6  
append 9  
append 1  
sort  
print  
pop  
reverse  
print  
list1=[]
def performListOperations(command, value=None):
    if command == "insert":
        index, val = value
        print("insert " + str(val) + " at Index "+ index)
        list1.insert(int(index), int(val))
        print(list1)
    elif command == "print":
        print(list1)
    elif command == "remove":
        value = value[0]
        if str(value) in list1:
            print("remove " + str(value) + " from the list:", list1)
            list1.remove(value)
        print("Value " + str(value) + " does not exist in Array: ", list1)
    elif command == "append":
        print("Append", list1)
    else: print("end.. some more code")

if __name__ == '__main__':
    N = int(input())
    for i in range(N):
        print('Which operation do you want to Perform: "insert", "print", "remove", "append","sort","pop","reverse"')
        getcmd, *value = input().split()
        print("Command:", getcmd, *value, value)
        performListOperations(getcmd, value)

You're inserting integers in the list when you do list1.insert(int(index), int(val)) , not strings.当您执行list1.insert(int(index), int(val))时,您是在列表中插入整数,而不是字符串。

So you need to use if int(value) in list1: and list1.remove(int(value))所以你需要使用if int(value) in list1:list1.remove(int(value))

There is a type mismatch.类型不匹配。 The insert is being done using integers, whereas, remove is looking for string type.插入是使用整数完成的,而删除是寻找字符串类型。 Hence, '5' is not found in the list as only 5 [int] is available.因此,在列表中找不到“5”,因为只有 5 [int] 可用。

Handle in one of the place, according to you convenience.根据您的方便,在其中一个地方办理。

below code worked以下代码有效


 elif command == "remove":
        value = value[0]
        print(value, list1)
        if int(value) in list1:
            print(value)
            print("remove " + str(value) + " from the list:", list1)
            list1.remove(int(value))
            return print(list1)
        print("Value " + str(value) + " does not exist in Array: ", list1)

暂无
暂无

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

相关问题 调用带有参数的 .Net 函数形式 python(通过引用传递) - Calling a .Net function form python which takes an argument (passed by reference) 调用函数作为参数传递而没有括号 - calling a function passed as an argument without parentheses 当作为参数传递给具有可变参数的函数时,列表解包如何工作? - How list unpacking works when passed as argument to function with variable parameters? 调用函数时的参数列表 - Argument list when calling a function 将参数传递给函数,该参数必须作为参数传递给python中的另一个函数 - Passing an argument to the function , which has to be passed as an argument to another function in python 这个函数如何传递一个没有参数的参数? - How is this function passed an argument with no parameter? In Pandas, how do I apply a function to a row of a dataframe, where each item in the row should be passed to the function as an argument? - In Pandas, how do I apply a function to a row of a dataframe, where each item in the row should be passed to the function as an argument? 填充作为参数传递给C函数的Python列表 - populate Python list passed as argument to C function Function 覆盖列表作为参数传入 - Python - Function overwriting list passed in as argument - Python 调用一个函数两次,但在第二次,函数返回的第一个列表作为 python 中的参数 - Calling a function twice but in second time, first list which was returned by the function goes as argument in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM