-1

I want to print the items with the index number in the output but I can't do it. This is how my script works:

1.Takes a string from the user and removes periods and commas. 2. Splits the string into a list called reshte.. 3. Delete the First Item from the list at index 0. 4. Scan the list whose first charter is capital then displays it in output.

Input:

The Persian League is the largest sport event dedicated to the deprived areas of Iran. The Persian League promotes peace and friendship. This video was captured by one of our heroes who wishes peace.

My code:

bs = [',','.']
reshte = input()
for s in bs:
    if s in reshte:
        reshte = reshte.replace(s,'')

reshte = reshte.split()
b = reshte[0]
css = [reshte.remove(b) for k in reshte if b in reshte]


for f in reshte:
    if f[0] == f[0].upper():
       print(f)

Output:

Persian
League
Iran
Persian
League

Expected output:

2:Persian
3:League
15:Iran
17:Persian
18:League
Mobodev
  • 55
  • 5
  • Show us what you have tried! – Klaus D. Dec 28 '20 at 20:38
  • 1
    I don't think the tags `project` and `z-index` are at all relevant to your post. Please remove them – Nano Tellez Dec 28 '20 at 20:43
  • One way to achieve this will be to find all indices of elements in list using [How to find all occurrences of an element in a list](https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list) , store the result in another dict. and pop first index from dict on each occurrence of element – Moinuddin Quadri Dec 28 '20 at 20:59
  • Please [edit] your question and add the input that's giving you that output. – martineau Dec 28 '20 at 21:09

2 Answers2

1

@Patrick did gave you an answer but then you said that is wrong, I assume that the results of the index is supposed to be index before removing the firs element in the list (step 3) and adding 1 to the index, because the results seems the index as we normally count and not as they are counted in Python (or most of Programming languages, see below for Guides).

I also see that in the result there isn't the word 'This' so I assume that you want ALSO to skip all words that starts with capital and are the same as the first element of list, so the word 'The'.

1. Using a List as result

test_string = """The Persian League is the largest sport event dedicated, \
to the deprived, areas of Iran. The Persian League promotes, peace and friendship.  
This video was captured by one of our heroes, who wishes peace.
"""

bs = [',', '.']
result_2 = []
# ========== < REMOVE COMMAS AND DOTS > ========= #
for punct in bs:
    if punct in test_string:
        test_string = test_string.replace(punct, '')


# ========== < STORE FIRST ELEMENT AT INDEX 0 > ========= #
reshte = test_string.split()
first_element = reshte[0]


# ========== < COUNTS ELEMENTS IN LIST > ========= #
results = []
for idx, f in enumerate(reshte):
    if first_element[0] == f[0]: # NOTE: we don't remove the first element but only skipping it all in once
        continue
    if f[0].isupper():
        results.append((idx, f))

for idx, value in results:
    print(f'{idx+1}:{value}')

# 2:Persian
# 3:League
# 15:Iran
# 17:Persian
# 18:League

2. Using a Dictionary

results = dict()
for idx, f in enumerate(reshte):
    if first_element[0] == f[0]: # NOTE: we don't remove the first element
        continue
    if f[0].isupper():
        results[idx] = f

for key, value in results.items():
    print(f'{key+1}:{value}')
    
# 2:Persian
# 3:League
# 15:Iran
# 17:Persian
# 18:League

print(results)

# {1: 'Persian', 2: 'League', 14: 'Iran', 16: 'Persian', 17: 'League'}

3. Count Duplicate of list

# ========== < FIND UNIQUE ELEMENT IN LIST > ========= #
unique_string = {string for string in reshte if string[0].isupper()}
results = []
# ========== < COUNT ELEMENTS  > ========= #
for element in unique_string:
    if first_element[0] == element[0]:
        continue
    element_count = reshte.count(element)
    results.append((element_count, element))

for idx, value in results:
    print(f'{idx}:{value}')
# 1: Iran
# 2: League
# 2: Persian

4. Use Counter

from collections import Counter

counted = Counter(reshte)

for element, count in counted.items():
    if element[0] == first_element[0]:
        continue
    elif element[0].isupper():
        print(f'{count}:{element}')
        
# 2:Persian
# 2:League
# 1:Iran

Guides & Documentations

Stack Overflow related questions

Federico Baù
  • 6,013
  • 5
  • 30
  • 38
0

Solve this using enumerate and the sep option of print. Just change the last for loop.

for i, f in enumerate(reshte):
    if f[0] == f[0].upper():
       print(i, f, sep=":")
Patrik
  • 107
  • 1
  • 7