There is a list:

['Новая', 'винтовка', '«Койот»!', '', 'Друзья,', 'оружейник', 'не', 'спит.', 'В', 'скором', 'времени', 'у', 'вас', 'появится', 'отличная', 'возможность', 'пострелять', 'из', 'новой', 'пушки.', 'Поскольку', 'винтовка', 'Мосина', 'была', 'сдвинута', 'с', '13', 'на', '16', 'уровень,', 'охотникам', 'требуется', 'новое', 'оружие,', 'доступное', 'на', '13-ом', 'уровне.', 'Место', '"Мосинки"', 'на', '13-ом', 'уровне', 'займет', 'винтовка', '"Койот",', 'которая', 'будет', 'использовать', 'калибр', '5.45.', 'Винтовка', 'будет', 'доступна', 'с', 'обновлением', '0.48.', '☝', '', '', '💀', 'Характеристики', '"Койота":', '•', 'Калибр:', '5.45х39', '•', 'Урон', 'винтовки', '(min-max):', '250-325', '', '•', 'Урон', 'патрона', '(min-max):', '60-140', '', '•', 'Общий', 'урон', '(min-max):', '310-465', '', '•', 'Бронебойность', 'патрона:', '15-25', '', '•', 'Бронебойность', 'оружия:', '5-10', '', '•', 'Кучность:', '1', 'MOA', '', '•', 'Вес:', '3.8', 'кг'] 

It is necessary to remove all special characters if they are not located in the center.

  • 2
    add to the list the expected result - gil9red
  • what does it mean in the "center"? What is the result for ['.'] . What do you refer to "special characters"? - jfs
  • I refer everything to special characters except letters. By "in the center", I understand, from the first Cyrillic character on the left to the extreme Cyrillic character on the right - Alexander Alexandrov

1 answer 1

You can try to exclude all characters with the regularity that do not match the listed ones:

 import re def my_strip(text): text = re.sub('^[^а-яА-ЯёЁ0-9a-zA-Z]+', '', text) text = re.sub('[^а-яА-ЯёЁ0-9a-zA-Z]+$', '', text) return text print([my_strip(x) for x in items]) 

Result:

 ['Новая', 'винтовка', 'Койот', '', 'Друзья', 'оружейник', 'не', 'спит', 'В', 'скором', 'времени', 'у', 'вас', 'появится', 'отличная', 'возможность', 'пострелять', 'из', 'новой', 'пушки', 'Поскольку', 'винтовка', 'Мосина', 'была', 'сдвинута', 'с', '13', 'на', '16', 'уровень', 'охотникам', 'требуется', 'новое', 'оружие', 'доступное', 'на', '13-ом', 'уровне', 'Место', 'Мосинки', 'на', '13-ом', 'уровне', 'займет', 'винтовка', 'Койот', 'которая', 'будет', 'использовать', 'калибр', '5.45', 'Винтовка', 'будет', 'доступна', 'с', 'обновлением', '0.48', '', '', '', '', 'Характеристики', 'Койота', '', 'Калибр', '5.45х39', '', 'Урон', 'винтовки', 'min-max', '250-325', '', '', 'Урон', 'патрона', 'min-max', '60-140', '', '', 'Общий', 'урон', 'min-max', '310-465', '', '', 'Бронебойность', 'патрона', '15-25', '', '', 'Бронебойность', 'оружия', '5-10', '', '', 'Кучность', '1', 'MOA', '', '', 'Вес', '3.8', 'кг'] 

Or through the strip function to describe which characters you want to remove.

Example:

 print('«Койот»!'.strip('«»!')) # Койот 

The code above would become something like this. You only need more special characters to describe:

 print([x.strip('«»!') for x in items]) 
  • Point in "5.45." not centered, but remained. - Enikeyschik
  • As a result, there is no (point) in response. Where did you get it from? - gil9red
  • The second is not, the first remained. And in "0.48" too. - Enikeyschik
  • As I understood from the question, the author did not mean right in the center, but wanted to remove special characters from both sides. However, I asked the result as he sees it, it will remain to wait. You can also clarify this question with the author - gil9red
  • If so interpreted, then yes. Perhaps even more correct interpretation. - Enikeyschik