raw_input()
返回字符串,而不是元组。你应该用
input()
而是使您的输入可以作为元组进行计算。你的
remove_tuple_from_list()
也不做任何删除
delete_element
从
list_tuple
因为这是比较
element[index]
到
删除元素
只是比较一下
element
到
删除元素
. 同时,它还为
列表
所以打电话的人
列表
永远不会更新。你应该更新
列表
用slice操作符代替:
list_tuple = [("100", 'AAA'), ("80", 'BBB'), ("20", 'CCC'), ("40", 'DDD')]
def remove_tuple_from_list(list_tuple,
delete_element):
list_tuple[:] = [element for element in list_tuple if not element == delete_element]
if __name__ == "__main__":
print "Before: list_tuple", list_tuple
print ""
delete_element = input('Enter Element to be deleted: ')
remove_tuple_from_list(list_tuple = list_tuple, delete_element = delete_element)
print "After: list_tuple", list_tuple
输入
("100", 'AAA')
,以上输出:
After: list_tuple [('80', 'BBB'), ('20', 'CCC'), ('40', 'DDD')]