3

I have to create this function that has as inputs a String and a list of strings; and as output a list of the indices of strings that contain the String. I have done it, but then I should ordinate the indices according to the occurrences of the String in the strings. How can i do that? This is my code: I added the 'count' under 'if' to count the occurrences, how can i use it to ordinate the indices according to that?

darkwidow
  • 55
  • 5
  • 1
    please input & expected output. you need `sort` with a key function but that's unclear what you're asking – Jean-François Fabre Nov 11 '17 at 17:35
  • 1
    why are you using count? please show us an sample input and output – yash Nov 11 '17 at 17:35
  • Example: function("c", ["hello", "cure", "access code"])---->output[2,1] instead now it returns me [1,2] – darkwidow Nov 11 '17 at 17:46
  • The function i create find the string s in each string of the list lst. It returns the indices of the list string where the string s is. But i have to ordinate that indices according to how many times the string s appears in the list string. Is it more clear now? – darkwidow Nov 11 '17 at 17:54

1 Answers1

1

You can add a list of counts in each string to your function,

def function(s,lst):
    l=[]
    counts = []
    for i in range(len(lst)):
        if s in lst[i]:
            counts += [lst[i].count(s)]
            l += [i]
    return l, counts

Here counts is a list in which each entry is the count of occurrences of s in the string in your input list. The function now returns two lists in a tuple, for example with the first tuple element being l and the second being counts. Note that i=-1 is redundant here as i is an element of the iterable made with range and assigning a value to it before the loop doesn't change it's loop value.

You can now sort the first list based on the second list using a line modified from this post,

out_fun = function(s,inp)
out = [x for x,_ in sorted(zip(out_fun[0],out_fun[1]), key = lambda x: x[1], reverse=True)]

inp is the list of strings, for example inp = ["hello", "cure", "access code"]. out_fun is the return tuple of two lists from the function function. s is the string of interest - here as in your original example it is 'c'.

What this line does is that it first creates a list of tuples using zip, where each first element of the tuple is is element from the list of indices and the second is from the list of occurrences. The program then sorts the tuples based on the second element in reverse order (largest first). The list comprehension fetches only the first element from each tuple in the sorted result, which is again the index list.

If you have questions about this solution, feel free to ask. You have a Python 2.7 tag - in Python 3.X you would need to use list(zip()) as zip returns a zip object rather than a list.


This is a more concise version of your program:

def function(s,lst):
    t = [(i,x.count(s)) for i,x in enumerate(lst) if s in x]
    return t

It uses a list comprehension to create and return a list of tuples t with first element being the index of the string that has the character s and second being the count. This is not necessarily more efficient, that would need to be checked. But it's a clean one-liner that at least to me is more readable.

The list of tuples can than be sorted in a similar way to the previous program, based on second tuple element i.e. count,

out_fun = function(s,inp)
out = [x for x,_ in sorted(out_fun, key = lambda x: x[1], reverse=True)]
atru
  • 4,699
  • 2
  • 18
  • 19
  • Thanks for your answer first of all. I am trying right now what you have written and I have few questions about this solution. First: out_fun is a new function? Second: in the first line code it is written ''out_fun = function(s,inp)'', what is ''inp'' in function? Third: to return out_fun I have to create a new function? Sorry for all these questions but I am really new about python(I am studying it since last month) and I know just few simple things about it. Thanks :) – darkwidow Nov 11 '17 at 20:42
  • No problem, my bad - I should have added the inputs. I'll update it now: out_fun is just the output from your function, inp is the input list of strings. – atru Nov 11 '17 at 21:00
  • I read it carefully and I understood now. I put out_fun in my function and it works. So really thanks. I'll be grateful to you forever :D – darkwidow Nov 11 '17 at 21:04
  • Good :) it was an interesting problem ;) It can also be solved in different, potentially more concise ways (the searching/counting part) - but this is good for the start, it shows you some useful components of Python. – atru Nov 11 '17 at 21:06
  • Really thanks again :D Just for information, how can be solved in the other way you said? – darkwidow Nov 11 '17 at 21:11
  • Updated :) I bet there's more. This one is what I would currently use. Also, read the edit about removing of i=-1 - it was redundant. i in the loop equals to values in the range (or other iterables) and is independent of the other i. – atru Nov 11 '17 at 23:11
  • REALLY THANKS for that. I appreciate what you have done for me. :D – darkwidow Nov 12 '17 at 09:28