【发布时间】:2021-02-27 05:24:07
【问题描述】:
这是我的示例数据库:
现在在我的脚本中,我使用以下代码向列表中添加元素:
import pandas
data = pandas.read_excel("Sample database copy.xlsx")
name = dict(zip(data["Abbreviation"],data["Name"]))
list1 = []
incoming_msg = input('Please type what you want to add: ')
incoming_msg = incoming_msg.split() # split the string by space
if len(incoming_msg) == 2: # if there are two elements in the list (number and name)
list1 += [name[incoming_msg[1]]] * int(incoming_msg[0])
else:
list1.append(name[incoming_msg[0]])
所以现在如果输入“2 JO”,我的列表将包含两个新元素“John”和“John”。
现在我想做完全相同的事情,但要消除列表中的对象。我试图将当前的运算符 "+=" 替换为 "-=" 但令人惊讶的是它不起作用。关于如何解决这个问题的任何想法?
PS。我需要从同一个输入中完成所有操作。我不能单独要求钥匙和数量。我想复制上述代码,但要删除对象。
【问题讨论】:
标签: python python-3.x pandas list