完成接受单个字符串参数文本的analyze_text()函数。您可以假设参数字符串的长度至少为1. 该函数返回一个包含两个项的列表,它们都是元组。第一个元组包含在参数文本中找到的惟一元音,按字母顺序排序。第二个元组包含在参数文本中找到的惟一辅音,按字母顺序排序。在英语中,元音是字母“A”,“E”,“I”,“O”和“U”。辅音是不是元音的字母。注意,大写字母和小写字母应该算作一个字母——“A”和“A”都可以算作“A”。下面显示了被调用函数的一些示例。例如:
输入:text = "Hello world!"
print("Text analysis:", analyze_text(text))
输出:Text analysis: [('e', 'o'), ('d', 'h', 'l', 'r', 'w')]
输入:text = "Hello world!"
text_analysis = analyze_text(text)
print(type(text_analysis))
print(type(text_analysis[0]))
print(type(text_analysis[1]))
输出:
输入: text = "I think, therefore I am."
print("Text analysis:", analyze_text(text))
输出:Text analysis: [('a', 'e', 'i', 'o'), ('f', 'h', 'k', 'm', 'n', 'r', 't')]
def analyze_text(text):
def analyze_text(strs):
import itertools as it
strs = [[1, i] if i in 'aeiou' else [2, i] for i in set(strs.lower()) if i >='a' and i <= 'z']
res = sorted(strs, key = lambda x: (x[0], x[1]))
res = it.groupby(res, key = lambda x: x[0])
res = [ tuple([k[1] for k in j])for i, j in res]
return res
text = "Hello world!"
print(analyze_text(text))
--result
[('e', 'o'), ('d', 'h', 'l', 'r', 'w')]