a = ['hello', 'worldw', 'eze'] # массив строк b = [s for s in a if s[0] == s[-1]] print(len(b)) # worldw eze
Бля, неправильно объяснил. Нужно чтоб были разные слова, и у этих слов совпадали первая и последния буквы. ['hello', 'word', 'hero'] - выведет 2: ['hello', 'hero']
toxicity, easy to write in python class Program { public static void Output(ref List<char[]> list) { foreach (var elements in list) { Console.WriteLine("{0} {1}",elements[0],elements[1]); Console.WriteLine(new string('*', 10)); } } public static int CheckArrayString(string[] result) { List<char[]> list = new List<char[]>(); List<string> listcontains = new List<string>(); for (int i = 0; i < result.Length; i++) { string line = result[i]; list.Add(new char[2]); list[i][0] = line[0]; list[i][1] = line[line.Length - 1]; } Console.WriteLine("Output Results "); Output(ref list); int count=0; int allresult = 0; for (int i = 0; i < list.Count; i++) { char start = list[i][0]; char end = list[i][1]; count=0; if (listcontains.Contains((start + end).ToString()) != true) { listcontains.Add((start + end).ToString()); for (int j = 0; j < list.Count; j++) { if (list[j][0] == start && list[j][1] == end) ++count; } allresult += (count > 1) ? count : 0; } } return allresult; } static void Main(string[] args) { string[] array = new string[] { "hello", "word", "hero","hero","melo","melo","melo","melo"}; Console.WriteLine(CheckArrayString(array)); } } Код class Program { public static void Output(ref List<char[]> list) { foreach (var elements in list) { Console.WriteLine("{0} {1}",elements[0],elements[1]); Console.WriteLine(new string('*', 10)); } } public static int CheckArrayString(string[] result) { List<char[]> list = new List<char[]>(); List<string> listcontains = new List<string>(); for (int i = 0; i < result.Length; i++) { string line = result[i]; list.Add(new char[2]); list[i][0] = line[0]; list[i][1] = line[line.Length - 1]; } Console.WriteLine("Output Results "); Output(ref list); int count=0; int allresult = 0; for (int i = 0; i < list.Count; i++) { char start = list[i][0]; char end = list[i][1]; count=0; if (listcontains.Contains((start + end).ToString()) != true) { listcontains.Add((start + end).ToString()); for (int j = 0; j < list.Count; j++) { if (list[j][0] == start && list[j][1] == end) ++count; } allresult += (count > 1) ? count : 0; } } return allresult; } static void Main(string[] args) { string[] array = new string[] { "hello", "word", "hero","hero","melo","melo","melo","melo"}; Console.WriteLine(CheckArrayString(array)); } } --- Сообщение объединено с предыдущим 9 дек 2019