社区所有版块导航
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学习  »  Python

在Python中,如何水平而不是垂直地执行此操作?

Ben • 5 年前 • 1449 次点击  

我在def函数中有这段代码,所以我只显示希望水平设置而不是垂直设置的代码。有人知道怎么做吗?我也希望没有括号。

values=random.randint(1,6), random.randint(1,6), random.randint(1,6), random.randint(1,6), random.randint(1,6)
   print("\nYou rerolled some dice and the new values are:")
   print("Die 1:", values[0]) 
   print("Die 2:", values[1]) 
   print("Die 3:", values[2]) 
   print("Die 4:", values[3]) 
   print("Die 5:", values[4])
   return values

输出:

You rerolled some dice and the new values are:
Die 1: 2
Die 2: 1
Die 3: 2
Die 4: 5
Die 5: 6

我希望它看起来怎么样

You rolled some dice and the new values are: 2, 1, 2, 5, 6
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50512
 
1449 次点击  
文章 [ 6 ]  |  最新文章 5 年前
accdias
Reply   •   1 楼
accdias    5 年前

为了换一种方式(更像是一种蟒蛇式的方式),下面是我的看法:

from random import randint


def roll_dice(n):
    return [randint(1, 6) for _ in range(n)]


print('You re-rolled some dice and the new values are:',
    ', '.join(map(str, roll_dice(5)))
)

或者,如果你想要更好的可视化 print() 学生:

print('You re-rolled some dice and the new values are: ', end='')
print(*roll_dice(5), sep=', ')

最后,如果您不关心不以逗号分隔的值,您可以简单地:

print('You re-rolled some dice and the new values are:', *roll_dice(5))

这里是这个概念的一个证明:

Python 3.7.5 (default, Oct 17 2019, 12:16:48) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from random import randint
>>> 
>>> 
>>> def roll_dice(n):
...     return [randint(1, 6) for _ in range(n)]
... 
>>> 
>>> print('You re-rolled some dice and the new values are:',
...     ', '.join(map(str, roll_dice(5)))
... )
You re-rolled some dice and the new values are: 4, 3, 5, 5, 4
>>> 
Darkstar Dream
Reply   •   2 楼
Darkstar Dream    5 年前

如果你想让你的输出在一行。你可以这样做

print(values[0],values[1],values[2],values[3],values[4],sep=',')

real_shardul
Reply   •   3 楼
real_shardul    5 年前

默认情况下,print()生成一个新行。而且您不需要在开始时使用。使用单个语句打印 占位符 使用。

占位符

  1. 数字 -%d个
  2. 一串 -%s个
    在字符串的末尾放置所需的值。

语法

num1=10
num2=20
print("single number: %d"%num1)
print("two numbers: %d %d"%(num1,num2)) # note the brackets after %

# Output
single number: 10
two numbers: 10 20

解决你的问题

print("\nYou rerolled some dice and the new values are: %d %d %d %d %d"%(values[0],values[1],values[2],values[3],values[4]))
adzo261
Reply   •   4 楼
adzo261    5 年前

你可以这样做:

print("\nYou rerolled some dice and the new values are: " + ", ".join(map(str, values)))

解释 ", ".join(map(str, values)) :

我们首先要做的是 mapping 这个 int 在里面 values str 然后 joining 使用 , 作为分隔符。

oppressionslayer
Reply   •   5 楼
oppressionslayer    5 年前

试试这个:

print("\nYou rerolled some dice and the new values are: {} {} {} {} {}".format(*values))

# You rerolled some dice and the new values are: 6 6 5 5 4


lbragile
Reply   •   6 楼
lbragile    5 年前
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))