When I make a list of strings, how do I define the limits of what I want to add to the first element, and what to the second and so on?

For example, I have a line:

'I am a person (1995) Russia' 

And my goal is to create such a list:

 ['I am man', '(1995)', 'Russia'] 

Closed due to the fact that the essence of the question by Nick Volynkin 11 Dec '16 at 6:14 is incomprehensible.

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • How can 'I am a person' get 'I am man' from 'I am a person' 'I am man' splitting a string? Did you read your question before posting it? - Nick Volynkin
  • как мне обозначить пределы того, что я хочу добавить к первому элементу, а что ко второму и так далее - first you need to formulate in simple Russian language the rules by which you will divide a line or select elements from it. A good problem statement is half the solution. - Nick Volynkin

1 answer 1

The answer to the question from the comment :

If before (1995) there is still some sentence in brackets, then it would be possible to somehow do this so that the program would divide the elements of the list only before (1995) and after? ( instead of 1995 can be any four-digit number )

Decision:

 In [7]: s = 'I (0) am a (smart) person (1995) Russia' In [8]: re.split(r'\s(\(\d{4}\))\s', s) Out[8]: ['I (0) am a (smart) person', '(1995)', 'Russia'] 

Description and analysis of this regular expression (in English)

  • I am very sorry but I had one problem. If before (1995) there is still some sentence in brackets, then it would be possible to somehow do so that the program would divide the elements of the list only before (1995) and after. (instead of 1995 there can be any four-digit number) - Bernard
  • one
    @Bernard, I corrected the answer - MaxU
  • four
    @MaxU this regular expression is applicable for specific cases when there are exactly four digits inside the brackets. And the answer does not explain how the expression is composed, therefore in this form it is useless (to everyone, including the author of the question, who will pass the homework, but will not learn anything). - Nick Volynkin
  • @NickVolynkin, I slightly corrected the answer and added a link to RegEx's step-by-step explanation (in English). - MaxU