1

How the program works

  1. Takes a string from the user

  2. Deletes it if there is a period or comma inside that string

3.split() that string into a list named reshte.

  1. Deletes the first item from the list.

  2. It takes the items whose first name is written in capital letters with for loop then displays the results.

I want to print the items with the index number in the output but I can't.

The input I give to the program:

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)

This is the output of my code == wrong:

Persian
League
Iran
Persian
League

But I want the output to be as follows == right:

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

Please help me solve this problem

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

3 Answers3

0

Just change the last part so it prints the number:

for i,j in enumerate(reshte):
    if j[0] == j[0].upper():
       print(str(i + 2) + ':' + reshte[i])
Hadrian
  • 917
  • 5
  • 10
0

The problem is that when you remove items from the list, the index of the words you output will change. The easiest thing to do is remove the items afterwards, so that in the loop, the words still retain their original index. In doing so, you can simply add the condition f != b in the loop to filter out reshte[0] from being printed.

The following code achieves this. Note the first part of your code can be greatly simplified - there's no need to check if , or . actually occur in the string before doing the substitution -- replace is a no-op if the text doesn't occur in the string.

reshte = input()

reshte = reshte.replace(',', '').replace('.', '')
reshte = reshte.split()

b = reshte[0]

for i, f in enumerate(reshte):
    if f[0] == f[0].upper() and f != b:
        print(f'{i + 1}:{f}')

css = [reshte.remove(b) for k in reshte if b in reshte]
costaparas
  • 5,047
  • 11
  • 16
  • 26
0

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