-4

def main():

how to make the list on the below

[abc, cde, abc, fgh, ijk, cde, abc,ijk] 

to become the result on the below?

Input  Output
abc      3
cde      2
fgh      1
ijk      2

also, i want to ask: what if the number of words of the strings in the list is different, how can let the result become :

e.g.     Input  Output
         abcde    3
         cde      2
         fghergf  1
         ijk      2

main()

win
  • 19
  • 1

2 Answers2

0

Create a function with a list parameter. Return a dictionary of the list items and their counts.

def count_items(lst):
    """ Return counts of the list items. """

    counts = {item: lst.count(item) for item in lst}
    return counts

Now, call the function with a list and print its items and their counts in two columns, like so:

lst = ['abc', 'cde', 'abc', 'fgh', 'ijk', 'cde', 'abc', 'ijk']

print("{0: <15} {1}".format("Input", "Output")) # adjust column width as needed

for key, value in count_items(lst).items():
    print("{0: <17} {1}".format(key, value))

It prints:

Input           Output
abc               3
cde               2
fgh               1
ijk               2

If you've lists that are quite big, the code below is more efficient:

from collections import Counter

def count_items(lst):
    """ Return counts of the list items. """    
    return Counter(lst)    

lst = ['abc', 'cde', 'abc', 'fgh', 'ijk', 'cde', 'abc', 'ijk']

print("{0: <15} {1}".format("Input", "Output")) # adjust column width as needed

for key, value in count_items(lst).items():
    print("{0: <17} {1}".format(key, value))
srikavineehari
  • 2,502
  • 1
  • 11
  • 21
  • @win, the length of each word in your list can vary, the code above still works. – srikavineehari Oct 07 '17 at 08:43
  • @win, the number of words in your list can also vary. Call the function a couple times, passing it a different list each time. – srikavineehari Oct 07 '17 at 08:50
  • but I found that the numbers of the output cannot be displayed in a straight line if the number of words of the strings in the input are different. what function should I add to solve the problem? – win Oct 07 '17 at 09:07
  • @win, just made a change. See my comment in the code. You can adjust column width as needed. – srikavineehari Oct 07 '17 at 09:13
  • ic, but what if i don't know how many numbers of words does the user input in the list how can i adjust the column width as need? – win Oct 07 '17 at 09:17
  • @win, when you've a new list of words, pass it to the function and if the printing is off, adjust the column width until you get the desired result :) – srikavineehari Oct 07 '17 at 09:20
  • @win, for example, in `print("{0: <15} {1}".format("Input", "Output"))`, you can increase or decrease the number `15` – srikavineehari Oct 07 '17 at 09:24
  • You _could_ find the maximum string length and use that to calculate the column width. You can do one level of nesting in format specifiers, which allows you to make the field width dynamic. Eg, `'{0:<{1}} {2}'.format(key, width, value)`. – PM 2Ring Oct 07 '17 at 10:49
0

One line solution:

count=[i for i in one if i in input]

Full code :

one = ['abc', 'cde', 'abc', 'fgh', 'ijk', 'cde', 'abc', 'ijk']

input=str(input(""))


count=[i for i in one if i in input]

print("input : {} \n count : {}".format(input,len(count)))

Testing :

>> abc
input : abc 
count : 3

>> fghergf
input : fghergf 
count : 1

>> PauletcabcPauloknow
input : PauletcabcPauloknow 
count : 3
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88