N = int(input("Введите количество списков в списке:")) arr = [list(map(int, input("Введите числа во вложенном списке:").split())) for i in range (N)] print(arr) res = list(map(max, zip(*arr))) print(res) - I would like to see the result in the program when I admit I enter the elements -34 gh 87 56 I have ignored 'gh', and the input is needed not by one element, but all the elements should be entered at once and then the exception would work - user310802
|
1 answer
import re prompt = "Введите числа во вложенном списке:" arr = [list(map(int, re.findall(r'((?:\+|\-)?\d+)', input(prompt)))) for i in range (N)] Введите числа во вложенном списке:1 2 3 Введите числа во вложенном списке:-10 aaa 34 bbb -123 Введите числа во вложенном списке:-4 +5 blah-blah 6 In [14]: arr Out[14]: [[1, 2, 3], [-10, 34, -123], [-4, 5, 6]] re.findall() is a search function for all occurrences that satisfies a regular expression ( Regular Expression AKA RegEx )
r'((?:\+|\-)?\d+)' - a template for selecting all numbers that can, but do not have to, have a ( + or - ) sign before the number
- re.sub (r '[^ \ d \ s-]', '' please explain this construction - user310802
- I already noticed I wanted to tell you) - user310802
- @MaxU what is
(N)- S. Nick - I can answer instead of MaxU, N is the number of lists in the list - user310802
- @ S.Nick, yes, N is taken from the question - MaxU
|