Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法删除';某些';列表中的空项_Python_String_List_Replace_Element - Fatal编程技术网

Python 无法删除';某些';列表中的空项

Python 无法删除';某些';列表中的空项,python,string,list,replace,element,Python,String,List,Replace,Element,我编写代码已经有一段时间了,但最近我遇到了一个相互矛盾的问题。由于某种原因,在打印的“第三阶段”,列表中仍然有空项。这很奇怪,因为代码的一部分应该删除所有为空的项,但有些项仍然保留 我发现唯一的问题是字符串中包含两个逗号,它们连接在“,”和之间,不会被删除 不幸的是,我没有能够解决这个问题,也没有找到任何帮助我的情况。 提前感谢您提供的任何帮助 SpecificPlayerChat = "!get specific player chat log: , ,, ,,, username |

我编写代码已经有一段时间了,但最近我遇到了一个相互矛盾的问题。由于某种原因,在打印的“第三阶段”,列表中仍然有空项。这很奇怪,因为代码的一部分应该删除所有为空的项,但有些项仍然保留

我发现唯一的问题是字符串中包含两个逗号,它们连接在“
”和之间,不会被删除

不幸的是,我没有能够解决这个问题,也没有找到任何帮助我的情况。 提前感谢您提供的任何帮助

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!")
参考文献:,

您在迭代列表时正在修改列表。 试试这个:

usernamelist = [i for i in usernamelist if i]
在这种情况下,
if i
类似于
if len(i)>0
。通常,请记住
'==False

如果要保持循环,请使用
enumerate()


您的
为空
应该崩溃,因为在python中它是
None
。它可能没有被评估过。我会调查一下的。哦,糟糕,我在发布这篇文章时忘了删除它!但我不知道我在想什么,我只是累了。但是如果没有空部分,则
如果项为“”:
可以正常工作,但之后的操作不起作用。请尝试使用
usernamelist=[i for i in usernamelist if i]
(否则,您的程序将崩溃,因为您在尝试从中删除项的同一列表上进行迭代)。如果你仍然有“空”项,并且你没有控制你的输入,我猜你在那些字符串中有不可打印的字符,这就行了!谢谢!但是如果你不介意的话,你能回答我
usernamelist=[i for i in usernamelist if i]
是如何工作的吗?谢谢没问题,我会的!再次非常感谢!尝试我甚至尝试过探索这种方法,但不幸的是没有用。我还修复了写入
或项为空的错误。我的错!但这对问题没有任何影响。另外,
打印(“检测到空项目!”)
将重复显示它在列表中找到并删除了多少项目。如果您运行整个代码,它应该可以很好地为您打印所有内容。
for i, item in enumerate(usernamelist):
    if item == '': #you can use "if not item:" instead
        usernamelist.remove(item)
        print("     Empty Item Detected!")