English 中文(简体)
我怎样才能在该方案的名单中增加分离者?
原标题:How can I add separators to lists of numbers in this program?

我在一次演习中告诉我说,每个人都有偏好的数字。 当我来到人数不止一个的人时,我不能把人数放在一条线上,按 com子分开。 你们能否提出解决办法?

我的法典

favourite_numbers = {
    "ha": [20, 10, 4],
    "quynh": [2, 5],
    "nhung": [3, 10, 12],
    "giang": [10],
}

for name, numbers in favourite_numbers.items():
    if len(numbers) == 1:
        for number in numbers:
            print(f"
{name.title()} s favourite number is {number}.")
    elif len(numbers) >= 2:
        print(f"
{name.title()} s favourite numbers are:")
        for number in numbers:
            print(f"{number}")

人数众多的人的产出就好像这样。

Ha s favourite numbers are:
20
10
4

我期望这样做:

Ha s favourite numbers are: 20, 10, 4
问题回答

欢迎社区! 应迅速确定:

for name, numbers in favourite_numbers.items():
    if len(numbers) == 1:
        for number in numbers:
            print(f"
{name.title()} s favourite number is {number}.")
    elif len(numbers) >= 2:
        print(f"
{name.title()} s favourite numbers are:", end=" ")
        for number in numbers:
            # Solution
            print(f"{number}", end=" ")

仅将论点end<>em>作为“并且你做了! http://www.w3schools.com/python/ref_func_print.asp”rel=“nofollow noreferer”>here。

为了在方案内增加人数清单,你可以采取以下步骤:

  1. 确定你想要使用的适当分离器(例如 com、空间、hy等)。

  2. 查明贵方案的具体地点,以生成或显示人数清单。

  3. 修改该法典,将选定的每一数字分开。

具体实施将取决于你所使用的方案拟订语言。 以下是在 Python和Script添加分离器的例子:

Python: Suppose you have a list of numbers and you want to display them as a comma-separated string.

# Sample list of numbers
numbers = [1, 2, 3, 4, 5]

# Using the join() method to add a comma separator and convert the list to a string
separator =  ,  
result = separator.join(map(str, numbers))

# Output the result
print(result)

JavaScript: Suppose you have an array of numbers and you want to display them as a comma-separated string.

// Sample array of numbers
const numbers = [1, 2, 3, 4, 5];

// Using the join() method to add a comma separator and convert the array to a string
const separator =  ,  ;
const result = numbers.join(separator);

// Output the result
console.log(result);

在这两种例子中,使用<代码>join()方法,将清单内容合并为与指定分离者之间的单一体格。 <map( function (inpill) or join(> methods (in Java texts)确保每份数字在加入之前均转换成星体,因为join(>>方法预期示意图。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...