有条件地格式化字符串 - Python
Conditionally format a string - Python
我想获取一个字符串并对其进行格式化,以便我可以控制对其进行更改的次数。例如..
"This is an awesome string" 用 "a" 的替换方法“@”会给我...
"this is @n @wesome string" 但是我想说把 1 "a" 换成“@” 剩下的不用管..
"This is @n awesome string" 放置可以是随机的,但重要的是我要考虑我更换了多少个特定种类。有什么想法吗?
字符串替换函数采用可选的计数参数来控制要进行的最大替换次数
"This is an awesome string".replace("a","@") # "This is @n @wesome string"
"This is an awesome string".replace("a","@",1) # "This is @n awesome string"
如果您需要随机执行,我们可以编写一个函数来执行此操作
import random
def randreplace(str,c,c_replace,maxnum=0):
if maxnum >= str.count(c) or maxnum<1:
return str.replace(c,c_replace)
indices = [i for i,x in enumerate(str) if x==c]
replacements = random.sample(indices,maxnum)
st_pieces = (x if not i in replacements else c_replace for i,x in enumerate(str))
return "".join(st_pieces)
此函数采用要进行替换的字符串、要替换的字符、要替换的字符以及最大替换数(全部为 0)和 returns 字符串所需的随机替换次数。
random.seed(100)
randreplace("This is an awesome string","a","@",1) # "This is @n awesome string"
randreplace("This is an awesome string","a","@",1) # "This is an @wesome string"
randreplace("This is an awesome string","a","@",2) # "This is @n @wesome string"
randreplace("This is an awesome string","a","@") # "This is @n @wesome string"
以下函数可让您更改单个匹配字符:
def replace_a_char(text, x, y, n):
matched = 0
for index, c in enumerate(text):
if c == x:
matched += 1
if matched == n:
return text[:index] + y + text[index+1:]
return text
text = "This is an awesome string and has lot of characters"
for n in xrange(1, 10):
print replace_a_char(text, 'a', '@', n)
为您提供以下输出:
This is @n awesome string and has lot of characters
This is an @wesome string and has lot of characters
This is an awesome string @nd has lot of characters
This is an awesome string and h@s lot of characters
This is an awesome string and has lot of ch@racters
This is an awesome string and has lot of char@cters
This is an awesome string and has lot of characters
This is an awesome string and has lot of characters
This is an awesome string and has lot of characters
@Andrew Mayes 说,"I was looking to make it random ..."
import random
target = "a"
replacement = "@"
string = "This is an awesome string"
indicies = [index for index, character in enumerate(string) if character == target]
index = random.choice(indicies)
string = string[:index] + replacement + string[index + 1:]
让我们把它变成一个函数,它可以选择要进行多少次随机替换,returns 修改后的字符串和实际进行的替换次数(例如,您可能要求太多。)
def random_replace(string, target, replacement, instances):
indicies = [index for index, character in enumerate(string) if character == target]
replacements = min(instances, len(indicies))
random_indicies = random.sample(indicies, replacements)
for index in random_indicies:
string = string[:index] + replacement + string[index + 1:]
return string, replacements
一些用法示例:
>>> print(random_replace(string, "a", "@", 3))
('This is @n awesome string @nd has lot of char@cters', 3)
>>> print(random_replace(string, "a", "@", 10))
('This is @n @wesome string @nd h@s lot of ch@r@cters', 6)
>>> print(random_replace(string, "a", "@", 0))
('This is an awesome string and has lot of characters', 0)
我想获取一个字符串并对其进行格式化,以便我可以控制对其进行更改的次数。例如..
"This is an awesome string" 用 "a" 的替换方法“@”会给我...
"this is @n @wesome string" 但是我想说把 1 "a" 换成“@” 剩下的不用管..
"This is @n awesome string" 放置可以是随机的,但重要的是我要考虑我更换了多少个特定种类。有什么想法吗?
字符串替换函数采用可选的计数参数来控制要进行的最大替换次数
"This is an awesome string".replace("a","@") # "This is @n @wesome string"
"This is an awesome string".replace("a","@",1) # "This is @n awesome string"
如果您需要随机执行,我们可以编写一个函数来执行此操作
import random
def randreplace(str,c,c_replace,maxnum=0):
if maxnum >= str.count(c) or maxnum<1:
return str.replace(c,c_replace)
indices = [i for i,x in enumerate(str) if x==c]
replacements = random.sample(indices,maxnum)
st_pieces = (x if not i in replacements else c_replace for i,x in enumerate(str))
return "".join(st_pieces)
此函数采用要进行替换的字符串、要替换的字符、要替换的字符以及最大替换数(全部为 0)和 returns 字符串所需的随机替换次数。
random.seed(100)
randreplace("This is an awesome string","a","@",1) # "This is @n awesome string"
randreplace("This is an awesome string","a","@",1) # "This is an @wesome string"
randreplace("This is an awesome string","a","@",2) # "This is @n @wesome string"
randreplace("This is an awesome string","a","@") # "This is @n @wesome string"
以下函数可让您更改单个匹配字符:
def replace_a_char(text, x, y, n):
matched = 0
for index, c in enumerate(text):
if c == x:
matched += 1
if matched == n:
return text[:index] + y + text[index+1:]
return text
text = "This is an awesome string and has lot of characters"
for n in xrange(1, 10):
print replace_a_char(text, 'a', '@', n)
为您提供以下输出:
This is @n awesome string and has lot of characters
This is an @wesome string and has lot of characters
This is an awesome string @nd has lot of characters
This is an awesome string and h@s lot of characters
This is an awesome string and has lot of ch@racters
This is an awesome string and has lot of char@cters
This is an awesome string and has lot of characters
This is an awesome string and has lot of characters
This is an awesome string and has lot of characters
@Andrew Mayes 说,"I was looking to make it random ..."
import random
target = "a"
replacement = "@"
string = "This is an awesome string"
indicies = [index for index, character in enumerate(string) if character == target]
index = random.choice(indicies)
string = string[:index] + replacement + string[index + 1:]
让我们把它变成一个函数,它可以选择要进行多少次随机替换,returns 修改后的字符串和实际进行的替换次数(例如,您可能要求太多。)
def random_replace(string, target, replacement, instances):
indicies = [index for index, character in enumerate(string) if character == target]
replacements = min(instances, len(indicies))
random_indicies = random.sample(indicies, replacements)
for index in random_indicies:
string = string[:index] + replacement + string[index + 1:]
return string, replacements
一些用法示例:
>>> print(random_replace(string, "a", "@", 3))
('This is @n awesome string @nd has lot of char@cters', 3)
>>> print(random_replace(string, "a", "@", 10))
('This is @n @wesome string @nd h@s lot of ch@r@cters', 6)
>>> print(random_replace(string, "a", "@", 0))
('This is an awesome string and has lot of characters', 0)