python查找和替换列表项(如果存在子字符串)
我想替换包含某个子字符串的列表中的项。在这种情况下,包含任何形式的“NSW”(大写字母很重要)的项目应替换为“NSW=0”。原始条目是否为“NSW=500”或“NSW=501”并不重要。我可以找到列表项,但不知怎的,我在列表中找不到位置,所以我可以替换它?这是我想到的,但我替换了所有项目:python查找和替换列表项(如果存在子字符串),python,string,python-3.x,list,substring,Python,String,Python 3.x,List,Substring,我想替换包含某个子字符串的列表中的项。在这种情况下,包含任何形式的“NSW”(大写字母很重要)的项目应替换为“NSW=0”。原始条目是否为“NSW=500”或“NSW=501”并不重要。我可以找到列表项,但不知怎的,我在列表中找不到位置,所以我可以替换它?这是我想到的,但我替换了所有项目: from __future__ import division, print_function, with_statement my_list =["abc 123","acd 234","NSW = 500
from __future__ import division, print_function, with_statement
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
for index, i in enumerate(my_list):
if any("NSW" in s for s in my_list):
print ("found")
my_list[index]= "NSW = 0"
print (my_list)
any
不会为您提供索引,并且在每次迭代中都是真的。所以放下它
就我个人而言,我会使用三元组的列表理解来决定保留原始数据或替换为NSW=0
:
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
result = ["NSW = 0" if "NSW" in x else x for x in my_list]
结果:
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
any
不会为您提供索引,并且在每次迭代中都是真的。所以放下它
就我个人而言,我会使用三元组的列表理解来决定保留原始数据或替换为NSW=0
:
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
result = ["NSW = 0" if "NSW" in x else x for x in my_list]
结果:
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
简单的
列表理解
:
>>> ["NSW = 0" if "NSW" in ele else ele for ele in l]
#驱动程序值:
IN : l = ["abc 123", "acd 234", "NSW = 500", "stuff", "morestuff"]
OUT : ['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
简单的
列表理解
:
>>> ["NSW = 0" if "NSW" in ele else ele for ele in l]
#驱动程序值:
IN : l = ["abc 123", "acd 234", "NSW = 500", "stuff", "morestuff"]
OUT : ['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
另一个解决方案:
代码:
from __future__ import division, print_function, with_statement
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
for index in xrange(len(my_list)):
if "NSW" in my_list[index]:
my_list[index] = "NSW = 0"
print (my_list)
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
print ["NSW = 0" if "NSW" in _ else _ for _ in my_list]
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
输出:
from __future__ import division, print_function, with_statement
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
for index in xrange(len(my_list)):
if "NSW" in my_list[index]:
my_list[index] = "NSW = 0"
print (my_list)
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
print ["NSW = 0" if "NSW" in _ else _ for _ in my_list]
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
或者,您可以使用列表理解来实现以下目的:
代码:
from __future__ import division, print_function, with_statement
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
for index in xrange(len(my_list)):
if "NSW" in my_list[index]:
my_list[index] = "NSW = 0"
print (my_list)
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
print ["NSW = 0" if "NSW" in _ else _ for _ in my_list]
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
输出:
from __future__ import division, print_function, with_statement
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
for index in xrange(len(my_list)):
if "NSW" in my_list[index]:
my_list[index] = "NSW = 0"
print (my_list)
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
print ["NSW = 0" if "NSW" in _ else _ for _ in my_list]
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
另一个解决方案:
代码:
from __future__ import division, print_function, with_statement
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
for index in xrange(len(my_list)):
if "NSW" in my_list[index]:
my_list[index] = "NSW = 0"
print (my_list)
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
print ["NSW = 0" if "NSW" in _ else _ for _ in my_list]
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
输出:
from __future__ import division, print_function, with_statement
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
for index in xrange(len(my_list)):
if "NSW" in my_list[index]:
my_list[index] = "NSW = 0"
print (my_list)
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
print ["NSW = 0" if "NSW" in _ else _ for _ in my_list]
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
或者,您可以使用列表理解来实现以下目的:
代码:
from __future__ import division, print_function, with_statement
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
for index in xrange(len(my_list)):
if "NSW" in my_list[index]:
my_list[index] = "NSW = 0"
print (my_list)
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
print ["NSW = 0" if "NSW" in _ else _ for _ in my_list]
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
输出:
from __future__ import division, print_function, with_statement
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
for index in xrange(len(my_list)):
if "NSW" in my_list[index]:
my_list[index] = "NSW = 0"
print (my_list)
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
my_list =["abc 123","acd 234","NSW = 500", "stuff","morestuff"]
print ["NSW = 0" if "NSW" in _ else _ for _ in my_list]
['abc 123', 'acd 234', 'NSW = 0', 'stuff', 'morestuff']
如果您正在按索引执行替换,则实际上不需要
any
。去掉any
。相反,如果在i:中使用“NSW”,则使用,如果您正在执行索引替换,则确实不需要任何
。请删除任何
。相反,如果i:
中的“NSW”,请使用,对不起,我真的没有找到链接的线程。对不起,我真的没有找到链接的线程