题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
k = int(list1[-1]) # 获取K,转为整型数据
x = list1[-2] # 获取x
list_x = [i for i in x] # 把x转为字母列表,例如abc转为['a','b','c']
list_x = sorted(list_x, reverse=False) # 对字母列表排序,方便对比
list1 = list1[1:-2] # 获取要匹配单词的列表
count = 0 # 记录匹配到的单词数量
res = [] # 记录匹配到的结果单词列表
for word in list1: # 单词的字母列表排序后相等,且不等于原单词,即符合条件
list_i = [i for i in word] # 转为字母列表
list_i = sorted(list_i, reverse=False) # 排序
if list_x == list_i and x != word: # 如果两个字母列表相等,并且不等于原单词,符合条件
count += 1 # 数量累加
res.append(word) # 符合条件的单词加入到res列表中
res = sorted(res, reverse=False) # 题目要求对结果单词列表排序
print(count) # 输出匹配到的单词数量
if k <= len(res): # 如果k小于res长度,输出k索引的单词,注意索引要减1
print(res[k - 1])