Help me record the code with one line using the roster generator.
-
Tell me how this code is:
production_year_x, production_year_y = 2014, 2015 rus_name_x, rus_name_y = 'Terminator 1', 'Terminator 2'
if production_year_x is not None:
main_x = rus_name_x + ' | ' + str(production_year_x)
else:
main_x = rus_name_x + ' | 'if production_year_y is not None:
compare_y = {site_y: rus_name_y + ' | ' + str(production_year_y)}
else:
compare_y = {site_y: rus_name_y + ' | '}
Do you want me to write one line through the list generator?
I've been able to write this code, but I can't drive it crazy.
main_x, compare_y,a,b = [name + ' | ' + str(year) for name in [rus_name_x, rus_name_y] for year in [production_year_x, production_year_y]]
In this case, the result is:
- main_x = 'Terminator 1 ' 2014'
- compare_y = 'Terminator 2 ' 2015'
-
I've got the lists on the individual variables because if I don't, the list generator very long.
production_year_x, production_year_y = 2014, 2015 rus_name_x, rus_name_y = 'Terminator 1', 'Terminator 2' # names = [rus_name_x, rus_name_y] years = [production_year_x, production_year_y] main_x, compare_y = [names[years.index(year)] + ' | ' + str(year) if year is not None else names[years.index(year)] + ' | ' for year in years] print(main_x, '\n', compare_y) Terminator 1 | 2014 Terminator 2 | 2015
production_year_x, production_year_y = 2014, None
Terminator 1 | 2014
Terminator 2 |
production_year_x, production_year_y = None, 2015
Terminator 1 |
Terminator 2 | 2015
production_year_x, production_year_y = None, None
Terminator 1 |
Terminator 2 |
The traditional type of generator:
for name in [rus_name_x, rus_name_y]:
i = [rus_name_x, rus_name_y].index(name)
year = [production_year_x, production_year_y][i]
if year is not None:
x = name + '|' + str(year)
else:
x = name + '|'
print(x)