无法删除列表中的 'certain' 个空项
Unable to remove 'certain' empty items inside list
我已经编码了一段时间,但最近我遇到了一个冲突问题。由于某种原因,在打印的 "Third Stage" 上,列表中仍有空项。这很奇怪,因为一段代码应该删除所有为空的项目,但有些项目仍然存在。 . . .
我发现的唯一问题是包含两个逗号的字符串“,,
”和 that 之间不会被删除。
不幸的是,我无法解决这个问题,也没有找到任何可以帮助解决我的情况的方法。
预先感谢您提供的任何帮助!
SpecificPlayerChat = "!get specific player chat log: , ,, ,,, username | category"
#Removing all spaces found within the string
SpecificPlayerChat = SpecificPlayerChat.replace(" ","").strip().replace("\n","")
print("First Stage:", SpecificPlayerChat)
#Store the string username & category data only and split the rest into a "junk" string
junk,SpecificPlayerChatData = SpecificPlayerChat.split(":")
print("Second Stage:", SpecificPlayerChatData)
junk = "" #This string deleted the unwanted string
#Split the usernames and categories into two separate strings
usernames,categories = SpecificPlayerChatData.split("|")
print("Third Stage:", usernames)
#Split the multiple words or "no words" into an item each to be store in a list
usernamelist = []
categorylist = []
usernamelist = usernames.split(",")
categorylist = categories.split(",")
#remove any empty items
print(" Print Empty items:")
for item in usernamelist:
if item is '':
usernamelist.remove(item)
print(" Empty Item Detected!")
print("Third Stage ", usernamelist)
这幅画有展示吗?
print(" Empty Item Detected!")
试试这个而不是删除:
将此替换为:
for item in usernamelist:
if item is '' or item is null:
usernamelist.remove(item)
print(" Empty Item Detected!")
有了这个:
for item in usernamelist:
if item is '' or item is null:
del usernamelist[usernamelist.index(item)]
print(" Empty Item Detected!")
参考资料:https://www.w3schools.com/python/ref_keyword_del.asp,
https://www.programiz.com/python-programming/methods/list/index
您正在迭代列表的同时修改它。
试试这个:
usernamelist = [i for i in usernamelist if i]
在这种情况下 if i
就像 if len(i) > 0
。一般来说,记住 '' == False
.
如果你想保持循环,那么使用enumerate()
for i, item in enumerate(usernamelist):
if item == '': #you can use "if not item:" instead
usernamelist.remove(item)
print(" Empty Item Detected!")
我已经编码了一段时间,但最近我遇到了一个冲突问题。由于某种原因,在打印的 "Third Stage" 上,列表中仍有空项。这很奇怪,因为一段代码应该删除所有为空的项目,但有些项目仍然存在。 . . .
我发现的唯一问题是包含两个逗号的字符串“,,
”和 that 之间不会被删除。
不幸的是,我无法解决这个问题,也没有找到任何可以帮助解决我的情况的方法。 预先感谢您提供的任何帮助!
SpecificPlayerChat = "!get specific player chat log: , ,, ,,, username | category"
#Removing all spaces found within the string
SpecificPlayerChat = SpecificPlayerChat.replace(" ","").strip().replace("\n","")
print("First Stage:", SpecificPlayerChat)
#Store the string username & category data only and split the rest into a "junk" string
junk,SpecificPlayerChatData = SpecificPlayerChat.split(":")
print("Second Stage:", SpecificPlayerChatData)
junk = "" #This string deleted the unwanted string
#Split the usernames and categories into two separate strings
usernames,categories = SpecificPlayerChatData.split("|")
print("Third Stage:", usernames)
#Split the multiple words or "no words" into an item each to be store in a list
usernamelist = []
categorylist = []
usernamelist = usernames.split(",")
categorylist = categories.split(",")
#remove any empty items
print(" Print Empty items:")
for item in usernamelist:
if item is '':
usernamelist.remove(item)
print(" Empty Item Detected!")
print("Third Stage ", usernamelist)
这幅画有展示吗?
print(" Empty Item Detected!")
试试这个而不是删除:
将此替换为:
for item in usernamelist:
if item is '' or item is null:
usernamelist.remove(item)
print(" Empty Item Detected!")
有了这个:
for item in usernamelist:
if item is '' or item is null:
del usernamelist[usernamelist.index(item)]
print(" Empty Item Detected!")
参考资料:https://www.w3schools.com/python/ref_keyword_del.asp, https://www.programiz.com/python-programming/methods/list/index
您正在迭代列表的同时修改它。 试试这个:
usernamelist = [i for i in usernamelist if i]
在这种情况下 if i
就像 if len(i) > 0
。一般来说,记住 '' == False
.
如果你想保持循环,那么使用enumerate()
for i, item in enumerate(usernamelist):
if item == '': #you can use "if not item:" instead
usernamelist.remove(item)
print(" Empty Item Detected!")