0
我有這樣一段代碼:如何將for循環轉換爲Python中的理解列表?
unique_char = np.zeros(26,dtype=np.int);
for char in s1:
unique_char[np.int(ord(char)-97)] += 1
這是整個代碼:
def check_permutation(str1,str2):
if str1 is None or str2 is None:
return False
if len(str1) != len(str2):
return False
s1 = str1.lower()
s2 = str2.lower()
unique_char = np.zeros(26,dtype=np.int);
for char in s1:
unique_char[np.int(ord(char)-97)] += 1
for char in s1:
unique_char[np.int(ord(char)-97)] -= 1
for x in unique_char:
if unique_char[x] != 0:
return False
return True
我怎麼能轉換成一個修真名單呢?
謝謝!
您似乎試圖計算字符串中字符出現次數 - 您是否考慮過使用[Counter](https://docs.python.org/2/library/collections.html#collections.Counter) ? – asongtoruin
你能粘貼完整的代碼和你想要的輸入/輸出的描述嗎? s1中有什麼? – papey
你可以,但它不會有效。在計數時,不要使用列表理解,使用'Counter()'。 –