社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  lbragile  »  全部回复
回复总数  1
5 年前
回复了 lbragile 创建的主题 » 在Python中,如何水平而不是垂直地执行此操作?
import random 

N = 5 # number of dice throws
values = [0] * N
for i in range(N):
    values[i] = random.randint(1,6)

# Remove brackets
str_values = [str(i) for i in values] # convert to strings
new_values = ", ".join(str_values)
print("\nYou rerolled some dice and the new values are: {}".format(new_values))

样本输出:

You rerolled some dice and the new values are: 1, 1, 6, 1, 5

如果需要返回数组值的函数(所有3种类型),请使用以下命令:

import random 

def calcVals(values, N):
    for i in range(N):
        values[i] = random.randint(1,6)

    # Remove brackets
    str_values = [str(i) for i in values] # convert to strings
    new_values = ", ".join(str_values)

    return values, str_values, new_values

N = 5 # number of dice throws
values = [0] * N

values, str_values, new_values = calcVals(values, N)
print("\nYou rerolled some dice and the new values are: {}".format(new_values))