It is necessary to output the entered values ​​in the reverse order, which can be both numbers and lines or lists.
I tried to do initially through:

import sys n = (sys.argv[1:]) print ' '.join(n[::-1]) 

However, brackets, commas, etc. are saved in this way.
For example, when you enter ['force', 'the', 'Feel'] , the following should be displayed: Feel the force .
With qwe asd zxc 123 - 123 zxc asd qwe .

  • one
    print ' '.join(['force', 'the', 'Feel'][::-1]) displays Feel the force without brackets and commas. - jfs

1 answer 1

 import sys import re input_line = ' '.join(sys.argv[1:]) output_line = ' '.join(re.split('\W+', input_line)[::-1]) print output_line.strip() 

For your example work. In general, the task is not entirely correct. My script first merges all the command line arguments into a single line separated by a space, then splits the resulting string into all "non-dictionary characters". It is not clear what you want to get when you run the python script.py for+ce the Fe-el program python script.py for+ce the Fe-el ? In this case, my option will be el Fe the ce for , + not part of the word. Simply if you run python script.py ['feel', 'the', 'force'] then the "input data" will be the lines [feel , the, force] , at least in * nix.

Another question to backfill:

 python script.py [1, 2, [3, 4, 5]] 

Result:

 a) [3, 4, 5] 2 1 b) [[5, 4, 3], 2, 1] c) 5 4 3 2 1 

All three options, to one degree or another, are suitable.

P.S. Regular expressions are one of the most difficult topics for a beginner, sorry.

Courses that offer

 list.reverse() result = list[0] for item in list[1:]: result += ' ' + item 

instead

 result = ' '.join(list[::-1]) 

should be immediately burned at the stake of the sacred inquisition

  • The conditions of the problem are not mine. Such is indicated in one of the courses. Just how many options I have not tried, I couldn’t manage to derive only the words when I entered the list, getting rid of the extra characters. - mantebose
  • Just my script and rearrange the "only words" to the best of understanding by the python term "word". Words can consist of letters, numbers, and the _ character, separated by any other characters. - andy.37
  • Since there are no symbols in the conditions, your option is suitable. But, in general, the response of the course makers is different. - mantebose
  • args = sys.argv [1:] args.reverse () result = args [0] for item in args [1:]: result = result + '' + item print result - mantebose
  • It will not remove punctuation marks. Without the use of regulars do not remove them. Just do for a in sys.argv[1:]: print a and see what is written to sys.argv in various ways. It's just the whole string, separated by whitespace. - andy.37 4:44