【问题标题】:How to get a different output?如何获得不同的输出?
【发布时间】:2020-11-24 12:36:19
【问题描述】:

我是 python 编程的新手。我正在尝试编写一个程序来查找列表中有多少个 9。enter image description here

seq = [1, 2, 3, 4, 9, 6, 8, 13, 9, 12, 19]
n = 0
t = []
y = len(seq)
for x in range (0, y-1):
    if seq[x] == 9:
        n += 1
        t.append(x+1) 
    else: continue
print (n, "numbers of 9s are present at", t, "position" )'

输出:[5, 9] 位置出现 2 个 9

我如何编写程序以获取输出为“在 5 和 9 位置存在 2 个 9”。

【问题讨论】:

  • ' and '.join(map(str, t)) 代替t 怎么样?

标签: python list output


【解决方案1】:

您可以使用列表推导。 enumerate 返回列表的元素 (num) 和元素的索引 (idx),如果元素 (num) 等于 9,则 positions 列表将包含 idx 作为细绳。索引从 1(不是 0)(start=1)开始。 len(positions)可以得到9的个数," and ".join(positions)可以加入元素。

代码:

seq = [1, 2, 3, 4, 9, 6, 8, 13, 9, 12, 19]
positions = [str(idx) for idx, num in enumerate(seq, start=1) if num == 9]
print("{} numbers of 9s are present at {} positions".format(len(positions), " and ".join(positions)))

输出:

>>> python3 test.py 
2 numbers of 9s are present at 5 and 9 positions

我想这是解决您的问题的最优雅的方法。

【讨论】:

    【解决方案2】:
    your_list = [4,5,6,7,9,6,7,9]
    indexes = []
    for i, num in enumerate(your_list, start=1):
        if num == 9:
            indexes.append(i)
    
    def output_list(str_list):
        last_value = str_list[-1]
        first_indexes = str_list[:-1]
        first_indexes_str = ", ".join(first_indexes)
        if len(first_indexes) == 0:
            return f'{len(indexes)} numbers of 9s are present at {last_value} position'
        return f'{len(indexes)} numbers of 9s are present at {first_indexes_str} and {last_value} position'
    
    print(output_list(indexes))
    

    索引列表的第一个空列表。

    遍历列表并使用(enumerate)获取索引。

    如果 num 为 9,则追加到索引列表中。

    之后使用f'...' 打印并打印找到的数量和位置。

    并根据需要添加解析数据的函数

    【讨论】:

    • 我猜你必须做indexes.append(i + 1) 才能得到想要的结果
    • 使用enumerate(your_list, start=1),这样你就可以直接得到正确的索引了。
    • 谢谢。但它给出的输出与我从程序中得到的输出相似。
    • 我以为你的算法有问题,而不是打印本身
    【解决方案3】:

    好吧,首先在列表中创建一个 for 循环,获取每个元素,然后将此元素与 9 进行比较,看看它是否为 9。 compt 变量将计数“9”次遭遇。

    compt = 0
    seq = [1, 2, 3, 4, 9, 6, 8, 13, 9, 12, 19]    
    for k in seq:
        if(k == 9):
            compt += 1
    print(compt)
    

    如果您还想要找到 9 的索引/位置,则执行相同操作,循环索引并保留索引列表:

    compt = 0
    indexlist = []
    seq = [1, 2, 3, 4, 9, 6, 8, 13, 9, 12, 19]    
    for k in range(0, len(seq)):
        if(seq[k] == 9):
            compt += 1
            indexlist.append(k)
    print(str(compt) + " nines founds, in following positions : " + str(indexlist))
    

    【讨论】:

      【解决方案4】:

      您可以在more_itertools 模块中使用locate

      from more_itertools import locate
      
      arr = [1, 2, 3, 4, 9, 6, 8, 13, 9, 12, 19]
      indices = list(locate(arr, lambda a: a==9))
      

      您可以将+1添加到索引中的每个元素以获取位置

      【讨论】:

        【解决方案5】:
         seq = [1, 2, 3, 4, 9, 6, 8, 13, 9, 12, 19]
        
         positions = [i + 1 for i in range(len(seq)) if seq[i] == 9]
        
         print(f"{len(positions)} numbers of 9s are present at {positions} position")
        

        【讨论】:

        • 可以添加一些解释吗?
        猜你喜欢
        • 2022-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-30
        • 2023-04-02
        • 2021-04-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多