其他分享
首页 > 其他分享> > 我的字谜在哪里?

我的字谜在哪里?

作者:互联网

 1 # anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']), ['aabb', 'bbaa'])
 2 def anagrams(word,words):
 3     #your code here
 4     arr = []
 5     data = list(word)# 字符串转列表
 6     data.sort()#列表排序
 7     list1 = [str(i) for i in data]
 8     list2 = "".join(list1)# 列表转字符串
 9     for i in words:
10         data = list(i)
11         data.sort()
12         list1 = [str(i) for i in data]
13         list3 = "".join(list1)
14         print(list3)
15         if list3 == list2:
16             arr.append(i)
17     return arr
18 if __name__=="__main__":
19     arr = anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer'])
20     print(arr)
1 def anagrams(word,words):
2     from collections import Counter
3     return [e for e in words if Counter(e) == Counter(word)]
4 if __name__=="__main__":
5     arr = anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer'])
6     print(arr)

 

标签:__,arr,anagrams,list1,哪里,racer,字谜,data
来源: https://www.cnblogs.com/mougui/p/16646664.html