Given an array of text value - quantity:

[('21', 1), ('25', 1), ('28', 1), ('31', 1), ('Данных нет', 14)] 

It is necessary to calculate the percentage of each value in general. The result should be:

 ['21', '1 / (1 + 1 + 1 + 1 + 14)'; '25', '1 / (1 + 1 + 1 + 1 + 14)'; '28', '1 / (1 + 1 + 1 + 1 + 14)'; '31', '1 / (1 + 1 + 1 + 1 + 14)'; 'Данных нет', '14 / (1 + 1 + 1 + 1 + 14)'] 

where 1/18 - это расчет процентов .

There is such a view on SQL:

 CREATE ALGORITHM = UNDEFINED DEFINER = `user`@`%` SQL SECURITY DEFINER VIEW `users_by_age` AS SELECT `list_of_users`.`age` AS `age`, COUNT(0) AS `number` FROM `list_of_users` GROUP BY `list_of_users`.`age` 

You can add 1 more column, but how is the question.

  • Okay, and what you can’t do, describe what the problem is? - Kromster
  • Yes, I can’t even think up the logic of the code in general, there was an idea somehow through the cycle ... - Alexander Alexandrov
  • Specify solutions you need to find on Python or on SQL? - Kromster
  • It doesn’t matter much; the data will eventually be added to the table excell - Alexander Alexandrov
  • @AlexanderAlexandrov, what is the structure as a result? If the list, then what is a semicolon? - MaxU

2 answers 2

  1. Count the total amount of members (18).
  2. For each group, divide the amount of its members into a total amount - this is your percentage.

In SQL it will be the same. Consider the sum in a variable as the first query, and the second - output it by groups divided by the sum found.

  • I understand how interest is calculated, the question of how to write it in a python - Alexander Alexandrov
  • You said - "I can’t even come up with a general logic of the code." I answered you in general. Clarify the question) - Kromster
  • I even generally can not understand the coding logic - Alexander Alexandrov
  • Try to complete the first item - count the sum of all members. In Python, you will probably do this from the first to the last cycle. In SQL, this is done with the COUNT () statement. Google about that and other options. - Kromster
  • I will count it. But what to do next is a good question - Alexander Alexandrov

It turned out everything is simple:

 for a in age: res_a = round((int(a[1])/count * 100),2) print(res_a) 
  • You forgot to clarify how and where count is taken. - Kromster