[英]How to remove certain elements from an array that start with certain characters?
I have a list of expressions, in an array known as literals
:我有一个表达式列表,位于一个称为literals
的数组中:
literals = ['-PacketAt(A)', '+CarAt(B)', '-CarAt(A)', '-PacketInCar', '+PacketAt(B)', '+CarAt(A)', 'LoadA', '+PacketAt(A)', '+PacketInCar', 'LoadB', '-PacketAt(B)', 'DriveAB', '-CarAt(B)', 'DriveBA', 'ProduceA', 'DischargeA', 'DischargeB']
I would like to remove the elements of the array that do not start with a +
and -
sign.我想删除不以+
和-
符号开头的数组元素。
I have written the following to try to do that:我写了以下内容来尝试这样做:
for literal in literals:
if not (literal.startswith('-')) and not (literal.startswith('+')):
literals.remove(literal)
However, after I run this for loop, I receive the following output:但是,在运行此 for 循环后,我收到以下输出:
literals = ['-PacketAt(A)', '+CarAt(B)', '-CarAt(A)', '-PacketInCar', '+PacketAt(B)', '+CarAt(A)', '+PacketAt(A)', '+PacketInCar', '-PacketAt(B)', '-CarAt(B)', 'ProduceA', 'DischargeB']
The desired output is:所需的输出是:
literals = ['-PacketAt(A)', '+CarAt(B)', '-CarAt(A)', ' -PacketInCar', '+PacketAt(B)',
'+CarAt(A)', '+PacketAt(A)', '+PacketInCar', '-PacketAt(B)', 'DriveAB', '-CarAt(B)']
From this, 'ProduceA'
and 'DischargeB'
should not be in the list known as literals
, but they are.由此, 'ProduceA'
和'DischargeB'
不应该在被称为literals
的列表中,但它们在。 Why is this so, and how can I modify my code such that they do not appear?为什么会这样,我如何修改我的代码以使它们不出现? Here is some runnable test code I have provided:这是我提供的一些可运行的测试代码:
literals = ['-PacketAt(A)', '+CarAt(B)', '-CarAt(A)', ' -PacketInCar', '+PacketAt(B)',
'+CarAt(A)', 'LoadA', '+PacketAt(A)', '+PacketInCar', 'LoadB', '-PacketAt(B)', 'DriveAB', '-CarAt(B)', 'DriveBA', 'ProduceA', 'DischargeA', 'DischargeB']
for literal in literals:
if not (literal.startswith('-')) and not (literal.startswith('+')):
literals.remove(literal)
print(literals)
and an online Python IDE to run it in: https://www.programiz.com/python-programming/online-compiler/以及运行它的在线 Python IDE: https : //www.programiz.com/python-programming/online-compiler/
The issue you're having is that your are trying to remove elements from a list while iterating the list.您遇到的问题是您试图在迭代列表时从列表中删除元素。 Try to avoid this.尽量避免这种情况。
You can use list comprehension to get the results:您可以使用列表理解来获得结果:
literals = ['-PacketAt(A)', '+CarAt(B)', '-CarAt(A)', ' -PacketInCar', '+PacketAt(B)', '+CarAt(A)', 'LoadA', '+PacketAt(A)', '+PacketInCar', 'LoadB', '-PacketAt(B)', 'DriveAB', '-CarAt(B)', 'DriveBA', 'ProduceA', 'DischargeA', 'DischargeB']
lst = [x for x in literals if x[0] in ['-','+']]
print(lst)
Output输出
['-PacketAt(A)', '+CarAt(B)', '-CarAt(A)', '-PacketInCar', '+PacketAt(B)', '+CarAt(A)', '+PacketAt(A)', '+PacketInCar', '-PacketAt(B)', '-CarAt(B)']
I will use a list comprehension:我将使用列表理解:
literals = ['-PacketAt(A)', '+CarAt(B)', '-CarAt(A)', '-PacketInCar', '+PacketAt(B)', '+CarAt(A)', 'LoadA', '+PacketAt(A)', '+PacketInCar', 'LoadB', '-PacketAt(B)', 'DriveAB', '-CarAt(B)', 'DriveBA', 'ProduceA', 'DischargeA', 'DischargeB']
literals = [x for x in literals if not x.startswith(('-','+'))]
print(literals)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.