通过 raw_input 替换列表中的重复项目
Replacing duplicate item in list through raw_input
对 Python 完全陌生,请多多包涵。
我通过 raw_input 创建了一个列表:
itemList = []
num = int (raw_input("Enter a number: "))
for i in range (num):
listStr = raw_input("Enter an item: ")
itemList.append (listStr)
现在我必须检查是否有任何项目已经存在,如果它确实要求将另一个 raw_input 添加到列表中。我完全被难住了。它没有循环;它只是打印一个 anyway.I 然后还必须将新项目附加到原始列表。难住了。
itemList = []
num = int (raw_input("Enter a number: "))
for i in range (num):
listStr = raw_input("Enter an item: ")
itemList.append (listStr)
for a in itemList:
if a in itemList :
a = raw_input("Enter another number: ")
这里是你的代码的一个稍微修改过的版本
itemList = []
num = int (raw_input("Enter a number: "))
for i in range (num):
listStr = raw_input("Enter an item: ")
itemList.append (listStr)
for idx in range(len(itemList)):
# using a while ensures that if the value newly entered
# is again present in the list, it again prompts for an input
while itemList[idx] in itemList[:idx] or itemList[idx] in itemList[idx+1:] :
a = raw_input("Enter a replacement for item {0}: ".format(itemList[idx]))
itemList[idx] = a
您可以使用 while
循环来不断请求输入,直到输入了一个不在列表中的项目。这可以改进,但它应该让你开始:
itemList = []
num = int (raw_input("Enter a number: "))
for i in range (num):
while True:
listStr = raw_input("Enter an item: ")
if listStr in itemList:
print('That item is already in the list')
else:
itemList.append(listStr)
break
稍微好一点的版本:
itemList = []
num = int(raw_input("Enter a number: "))
for i in range(num):
listStr = raw_input("Enter an item: ")
while listStr in itemList:
print("That item already exists")
listStr = raw_input("Enter another number: ")
itemList.append(listStr)
对 Python 完全陌生,请多多包涵。
我通过 raw_input 创建了一个列表:
itemList = []
num = int (raw_input("Enter a number: "))
for i in range (num):
listStr = raw_input("Enter an item: ")
itemList.append (listStr)
现在我必须检查是否有任何项目已经存在,如果它确实要求将另一个 raw_input 添加到列表中。我完全被难住了。它没有循环;它只是打印一个 anyway.I 然后还必须将新项目附加到原始列表。难住了。
itemList = []
num = int (raw_input("Enter a number: "))
for i in range (num):
listStr = raw_input("Enter an item: ")
itemList.append (listStr)
for a in itemList:
if a in itemList :
a = raw_input("Enter another number: ")
这里是你的代码的一个稍微修改过的版本
itemList = []
num = int (raw_input("Enter a number: "))
for i in range (num):
listStr = raw_input("Enter an item: ")
itemList.append (listStr)
for idx in range(len(itemList)):
# using a while ensures that if the value newly entered
# is again present in the list, it again prompts for an input
while itemList[idx] in itemList[:idx] or itemList[idx] in itemList[idx+1:] :
a = raw_input("Enter a replacement for item {0}: ".format(itemList[idx]))
itemList[idx] = a
您可以使用 while
循环来不断请求输入,直到输入了一个不在列表中的项目。这可以改进,但它应该让你开始:
itemList = []
num = int (raw_input("Enter a number: "))
for i in range (num):
while True:
listStr = raw_input("Enter an item: ")
if listStr in itemList:
print('That item is already in the list')
else:
itemList.append(listStr)
break
稍微好一点的版本:
itemList = []
num = int(raw_input("Enter a number: "))
for i in range(num):
listStr = raw_input("Enter an item: ")
while listStr in itemList:
print("That item already exists")
listStr = raw_input("Enter another number: ")
itemList.append(listStr)