Convert loop results into table. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Convert loop results into table.

I have a function running in loop which returns output like this- 24.1 40.2 24.1 63.4 40.2 24.1 and thousands of values and so on.. I want to result like below(unique values with count of their accurance). Any guidance with be appreciated - 24.1 3 40.2 2 63.4. 1 Thanks

22nd Apr 2020, 10:14 AM
Rohit Singh
Rohit Singh - avatar
5 Answers
+ 6
You can do this: - Take the values that are generated and append them in a list. - Use this list and a for loop to iterate all the lists, but be careful by doing this. it should look like this: #list might be "numbers" which lokks like that: [24.1,3.14,27.9,18.6,3.14,24.1] numbers = [24.1,3.14,27.9,18.6,3.14,24.1] for num in set(numbers): # this makes sure, that you count duplicated values correct print(f' {num} - {numbers.count(num)}') # result is: ''' 3.14 - 2 18.6 - 1 24.1 - 2 27.9 - 1 '''
22nd Apr 2020, 10:49 AM
Lothar
Lothar - avatar
+ 3
Like this? a=[24,40,24,64,40,24] b={i:a.count(i) for i in a} print(b)
22nd Apr 2020, 10:41 AM
Justus
Justus - avatar
+ 3
a=[24,40,24,64,40,24] b={i:a.count(i) for i in a} a=set(a) for i in a: print(i,":",b[i])
22nd Apr 2020, 10:43 AM
Justus
Justus - avatar
+ 3
Rohit Singh, you have not been talking about how the final data pairs will be used. Is it just to print them all ( or like first 100 or a selection with some criteria), or do you need to keep them in a file? If you have any doubt concerning the total number of pairs, you can use a generator. This will keep memory consumption low compared to the samples we have shown you.
22nd Apr 2020, 1:35 PM
Lothar
Lothar - avatar
0
I thought about using sets, but is it good idea to put output into list if my function results records between .5 millions to 1 million (excel sheet limit)?
22nd Apr 2020, 11:10 AM
Rohit Singh
Rohit Singh - avatar