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()'。 –