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()'を使います。 –