Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中水平而不是垂直地执行此操作?_Python_Python 3.x - Fatal编程技术网

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

如何在Python中水平而不是垂直地执行此操作?,python,python-3.x,Python,Python 3.x,我在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:", valu

我在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
试试这个:

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


样本输出:

如果希望函数返回所有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))
您可以这样做:

print("\nYou rerolled some dice and the new values are: " + ", ".join(map(str, values)))
.joinmapstr值的说明:
我们要做的是首先将int-in值映射到str,然后使用,作为分隔符进行连接。

print默认情况下会生成一个新行。您也不需要在开始中使用\n。要使用单个语句打印,请使用占位符

占位符 数字-%d 字符串-%s 在字符串的末尾放置所需的值。 语法 num1=10 num2=20 打印单号:%d%num1 打印两个数字:%d%d%num1,num2请注意% 输出 单号:10 两个号码:1020 解决你的问题 打印\n您重新掷了一些骰子,新的值为:%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d值[0]、值[1]、值[2]、值[3]、值[4]
如果您想在单行中获得输出。你可以这样做

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


只是想提出一种更具蟒蛇风格的替代方法,以下是我的看法:

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('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
>>> 

将值存储在数组中,然后打印array@ibragile,比如值=[1,2,3,4,5]?打印值[0],值[1],值[2],值[3],值[4]在您的情况下不起作用吗?@Ben请看下面我的答案。这里有一种更为类似Python的初始化值的方法:值=[random.randint1,6代表uuu5]。此代码将根据N个骰子预先分配值数组。然后还可以保留int的值数组或string的str_值数组。连接函数simple用于删除列表中的方括号。这正是我所要寻找的,我曾试图找到一种方法来使用范围内的for来实现这一点,但我无法找到它。谢谢你,伙计!这里有一种更类似python的初始化str_值的方法:str_值=[strrandom.randint1,6代表范围5中的u]。你也可以用“,”来删除所有这些行。加入[strrandom.randint1,6代表uu in range5]。@accdias,这是一个很好的方法,但我应该指出,答案中的当前方式除了str_值列表之外,还提供了整数列表。这可能对OP以后的代码很有用。@lbragile,只需返回整数列表并在函数外部处理即可。从理论上讲:简单比复杂好-
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
>>>