0

Code:

import numpy as np
arr = [np.array(['aaaa', '12a', '1a'],dtype=object),np.array(['a', '1a', '1bb'],dtype=object),np.array(['a', '1a', '1b'],dtype=object)]


for arr1 in arr:
    sum_list = []
    for a in arr1:  
        sum = 0
        for i in range(10):
            sum += a.count(str(i))
        sum_list.append(sum)
    print(arr1,"->",sum_list) 



Output:

['aaaa' '12a' '1a'] -> [0, 2, 1]
['a' '1a' '1bb'] -> [0, 1, 1]
['a' '1a' '1b'] -> [0, 1, 1]

Desired Ouput: 1. Item containing more numbers than other items. 2. In case there are more than 1 item containing same amount of numbers, take the item having bigger length. 3. In case there are more than 1 item containing same amount of numbers and having the same length, take the item having the first order.

['12a']
['1bb']
['1a']

Kindly let me know how to get desired output.

Thank you!

  • 1
    did you know you can customize the `max` function by specifying a `key`? [Have a look](https://stackoverflow.com/questions/18296755/python-max-function-using-key-and-lambda-expression). – Ma0 Dec 20 '19 at 09:32
  • Your code does not try to achieve what you want to get. Do you want us to solve this for you? That's not really how SO works - we help you get unstuck if your code is faulty, we do not code solutions for you. – Patrick Artner Dec 20 '19 at 09:38

1 Answers1

1

Try:

[max(i,key=len) for i in arr]

outputs:

['123a', '1bb', '1a']
Billy Bonaros
  • 1,671
  • 11
  • 18
  • Magic! Thank you Billy – sintelligence Dec 20 '19 at 09:44
  • Billy, I've tried to run it. The issue is it is getting the item with max length. Example: ['ABU', 'KASSS', '10KG'], it is getting 'KASSS' instead of '10KG". Kindly have a look again at my updated question and kindly let me know. I am really getting stuck at this. Thank you – sintelligence Dec 20 '19 at 10:00