Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.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_Python 2.7_List - Fatal编程技术网

如何删除python列表中字符串的一部分?

如何删除python列表中字符串的一部分?,python,python-2.7,list,Python,Python 2.7,List,我有以下代码输出(使用键盘模块): 如何从列表中删除每个“键盘事件”?尝试使用循环删除此事件: list = [KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)] for x in list: del list[str(x)]

我有以下代码输出(使用键盘模块):


如何从列表中删除每个“键盘事件”?

尝试使用循环删除此事件:

list = [KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)]

for x in list:
    del list[str(x)]
此列表压缩应该可以使用

使用以下方法如何:

为了获得更好的结果,您可以将此与以下内容结合使用:


演示:


我想试试正则表达式

import re

Foo = [KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)]

strList = []

for item in Foo:
  bar = re.sub('KeyboardEvent(\(.*?)', '', str(item))
  bar = re.sub('\)', '', bar)
  strList.append(bar)

print strList

或者,您也可以尝试将键盘事件作为字符串删除:

a=[KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)]
a=[str(elem).strip('KeyboardEvent') for elem in a]

请清楚您的列表元素。此列表中似乎没有字符串;相反,它们是对象(类KeyboardEvent的单个实例)。最简单的方法就是
list=[]
。是否有一个特定的事件包含您想要删除的某个键盘操作(如“enter up”)?如果它不是键盘事件怎么办?它仍然可以工作。它适用于删除列表/数组中的任何内容。然后你可以使用
list=[]
True,更简单。不建议在PythonCan中使用list作为变量名。不要责怪你完全按照他的要求去做,但这会产生一个空列表,这显然不是目标。@ChristianDean我不知道。你能解释一下原因吗?@ChristianDean,这就是你想要的吗?@Ajax1234当然。它是首选的,因为
实例
考虑了子类,而
类型
不考虑。这里是相关的。['(输入up),KeyboardEvent(h down),KeyboardEvent(h up),KeyboardEvent(e down),KeyboardEvent(e up),KeyboardEvent(y down),KeyboardEvent(y up)”,它只删除了第一个。效果很好,但我仍然喜欢@Vinícius Aguiar回答更多。不管怎样,还是向上投票。
newList = [event.name for event in myList]
newList = [event.name + ' ' + event.event_type for event in myList]
>>> myList
[KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down)]

>>> [event.name for event in myList]
['enter', 'h', 'h', 'e', 'e', 'y']

>>> [event.name + ' ' + event.event_type for event in myList]
['enter up', 'h down', 'h up', 'e down', 'e up', 'y down']
import re

Foo = [KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)]

strList = []

for item in Foo:
  bar = re.sub('KeyboardEvent(\(.*?)', '', str(item))
  bar = re.sub('\)', '', bar)
  strList.append(bar)

print strList
a=[KeyboardEvent(enter up), KeyboardEvent(h down), KeyboardEvent(h up), KeyboardEvent(e down), KeyboardEvent(e up), KeyboardEvent(y down), KeyboardEvent(y up)]
a=[str(elem).strip('KeyboardEvent') for elem in a]