2016-02-09 70 views
0

我想要一個字符串並對其進行格式化,以便我可以控制更改的數量。例如..有條件地格式化一個字符串 - Python

「這是一個真棒字符串」替換法「@」的「一」能給我...

「這是@n @wesome字符串」但是,我想說的替換1「a」帶「@」,剩下的只剩下其中一個..

「這是@n真棒字符串」放置允許隨機,但最重要的是我說明了我替換的特定種類的數量。有任何想法嗎?

回答

1

的字符串替換函數有一個可選的count參數來控制替換的最大數量,使

"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他們)並返回隨機完成所需數量替換的字符串。

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" 
+0

它是否在改變它涉及到或有任何特定的順序給它的第一個?我期待隨機使用,但我認爲我可以從中解決問題。謝謝! –

+0

是的,它將取代第一次出現。 – Matthew

+0

好吧,那是我的工作:) –

1

以下功能將讓你改變一個匹配的字符:

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 [email protected] lot of characters 
This is an awesome string and has lot of [email protected] 
This is an awesome string and has lot of [email protected] 
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 
+0

如果我將「n」更改爲不同的數字,這也可以嗎? –

+0

或者等不是「n」,但是我想匹配的數量是2或3「@」? –

1

@Andrew梅斯說,「我一直在尋找使它隨機...「

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:] 

讓我們把它變成一個函數,它可以選擇製作多少個隨機替換,並返回修改過的字符串和實際替換次數(例如,你可能會要求太多)

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 [email protected]', 3) 
>>> print(random_replace(string, "a", "@", 10)) 
('This is @n @wesome string @nd [email protected] lot of [email protected]@cters', 6) 
>>> print(random_replace(string, "a", "@", 0)) 
('This is an awesome string and has lot of characters', 0)