-1

我想在输出中打印带有索引号的项目,但我做不到。这就是我的脚本的工作方式:

1.从用户那里获取一个字符串并删除句点和逗号。2. 将字符串拆分为一个名为 的列表reshte.。3. 从索引 0 的列表中删除第一项。 4. 扫描第一个宪章是资本的列表,然后将其显示在输出中。

输入:

波斯联赛是致力于伊朗贫困地区的最大体育赛事。波斯联盟促进和平与友谊。这段视频是由我们的一位希望和平的英雄拍摄的。

我的代码:

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)

输出:

Persian
League
Iran
Persian
League

预期输出:

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

2 回答 2

1

@Patrick 确实给了你一个答案,但是你说那是错误的,我假设在删除列表中的第一个元素(步骤 3)并将 1 添加到索引之前,索引的结果应该是索引,因为结果似乎是我们通常计算的索引,而不是在 Python(或大多数编程语言,请参阅下面的指南)中计算的索引。

我还看到结果中没有“This”这个词,所以我假设您还希望跳过所有以大写开头并且与列表的第一个元素相同的词,所以“The”这个词。

1. 使用列表作为结果

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. 使用字典

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.计算列表的重复

# ========== < 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.使用计数器

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

指南和文件

堆栈溢出相关问题

于 2020-12-29T05:37:15.597 回答
0

使用 enumerate 和 print 的 sep 选项解决此问题。只需更改最后一个 for 循环。

for i, f in enumerate(reshte):
    if f[0] == f[0].upper():
       print(i, f, sep=":")
于 2020-12-28T21:26:31.840 回答