简体   繁体   English

在列表的每个项目之间加入元素 - python

[英]Join element between each item of a list - python

I want to join an element in this case it's "*" between each element of the list but not at the first position and not in the last position我想在这种情况下加入一个元素,它是列表的每个元素之间的“*”,但不是在第一个 position 和最后一个 position

How can I do this?我怎样才能做到这一点?

Code:代码:

import math
def decompose(n:int):
    
    factors = []
    
    
    if n < 2:
        return False
    
    for d in range(2, int(math.sqrt(n)) +1):
        
        if n % d == 0:
            factors.append(d)
            print(factors)
            
    factors = str(factors)
    
            
    print(f"The decomposition of {number5} in prime factor is" + '*'.join(str(factors)))       
    return True
    
number5 = int(input('Chose a number:'))


print(decompose(number5))

it prints this:它打印这个:

Decomposition of 45 in prime factor is [*3*,* *5*]

But that's not what I want, I want numbers without commas and without the * in first and last position但这不是我想要的,我想要在第一个和最后一个 position 中不带逗号且不带*的数字

This way:这边走:

def decompose(n: int):
    factors = []

    if n < 2:
        return False

    d = 2
    while d <= n:
        while n % d == 0:
            factors.append(str(d))
            n //= d
        d += 1
    if n > 1:
        factors.append(str(n))

    print(f"The decomposition of {number5} in prime factor is " + '*'.join(factors))
    return True


number5 = int(input('Chose a number:'))

print(decompose(number5))

What I've changed:我改变了什么:

  • The algorithm of factorization.分解算法。 Now it counts multiple multipliers.现在它计算多个乘数。
  • factors now is a List[str] , so join could be performed easily. factors现在是List[str] ,因此可以轻松执行join

One good way to return the factors from the method and then iterate the list as string value.从方法返回因子然后将列表迭代为字符串值的一种好方法。

import math


def decompose(n: int):
    factors = []
    if n < 2:
        return False
    for d in range(2, int(math.sqrt(n)) + 1):
        if n % d == 0:
            factors.append(d)
    return factors


number5 = int(input('Choose a number:'))
factors = decompose(number5)
print(f"The decomposition of {number5} in prime factor is " + '*'.join(
    [str(i) for i in factors]))

Output: Output:

Choose a number:45
The decomposition of 45 in prime factor is 3*5

There are different ways to do this, the following explanation is just one way.有不同的方法可以做到这一点,下面的解释只是一种方法。

In this line:在这一行:

factors = str(factors)

You are turning the list itself into a string.您正在将列表本身变成一个字符串。 You need to turn each item in the list into strings if you want to join them.如果要加入列表中的每个项目,则需要将它们转换为字符串。 You can do this by using a list comprehension:您可以通过使用列表推导来做到这一点:

factors = [str(x) for x in factors]

Then I would change the line where you print the decomposition to this:然后我会将打印分解的行更改为:

print(f"The decomposition of {number5} in prime factor is {'*'.join(factors)}.")

So with input 45 the output should be (amongst other things) "The decomposition of 45 in prime factor is 3*5".因此,对于输入 45,output 应该是(除其他外)“45 的素数分解是 3*5”。

EDIT: You might want to look at the rest of your code, 3*5 is not the prime decomposition of 45.编辑:您可能想查看代码的 rest,3*5 不是 45 的主要分解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM