Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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
函数将int转换为字符串以添加到映射python中_Python_Random_Map - Fatal编程技术网

函数将int转换为字符串以添加到映射python中

函数将int转换为字符串以添加到映射python中,python,random,map,Python,Random,Map,我昨天刚开始学习python。该任务是从中随机生成5种颜色 c=["red", "blue", "green", "yellow", "purple", "orange", "white", "black"] 所以我用 随机选择(c) 要创建一个50的列表,我想创建一个函数,然后使用一个函数添加随机颜色和迭代范围(1,51),将其添加到map() 将数字1、2、3、4等更改为随机选择的颜色 我不知道如何把这个数字变成一个随机数。选择(c)-那么我可以用什么函数来绘制地图呢 print map(

我昨天刚开始学习python。该任务是从中随机生成5种颜色

c=["red", "blue", "green", "yellow", "purple", "orange", "white", "black"]
所以我用 随机选择(c)

要创建一个50的列表,我想创建一个函数,然后使用一个函数添加随机颜色和迭代范围(1,51),将其添加到map()

将数字1、2、3、4等更改为随机选择的颜色

我不知道如何把这个数字变成一个随机数。选择(c)-那么我可以用什么函数来绘制地图呢

print map('function', range(1,51))
我考虑过像这样使用lambda

>>> sums = lambda x:(x-x)+(random.choice(c))
>>> print map(sums,range(1,51))
但是我有

Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    print map(sums,range(1,51))
  File "<pyshell#14>", line 1, in <lambda>
    sums = lambda x:(x-x)+(random.choice(c))
TypeError: unsupported operand type(s) for +: 'int' and 'str'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
打印地图(总和,范围(1,51))
文件“”,第1行,在
总和=λx:(x-x)+(随机选择(c))
TypeError:不支持+:“int”和“str”的操作数类型
但是它不起作用,因为我是字符串的整数

我试图通过文档查找一些东西,但没有运气


关于我能做什么的任何提示

你可以得到50种颜色,如下所示:

>>> c = ["red", "blue", "green", "yellow", "purple", "orange", "white", "black"]
>>> for _ in range(50):
...     random.choice(c)
...     
... 
'blue'
'red'
'yellow'
...
要生成50种颜色的列表,请使用列表:

>>> colors = [random.choice(c) for _ in range(50)]
>>> colors
['green', 'red', 'green', 'orange', 'orange', 'black', 'black', 'purple', 'red',
 'black', 'yellow', 'yellow', 'white', 'purple', 'orange', 'white', 'red', 'gree
n', 'black', 'blue', 'orange', 'green', 'orange', 'blue', 'white', 'blue', 'oran
ge', 'black', 'orange', 'green', 'black', 'green', 'blue', 'orange', 'blue', 'gr
een', 'green', 'white', 'red', 'green', 'yellow', 'yellow', 'blue', 'orange', 'p
urple', 'green', 'black', 'yellow', 'black', 'red']
您也可以像这样使用
map

>>> map(lambda _: random.choice(c), range(50))
['purple', 'yellow', 'black', 'white', 'black', 'black', 'purple', 'red', 'blue'
, 'white', 'blue', 'white', 'yellow', 'yellow', 'white', 'blue', 'orange', 'whit
e', 'yellow', 'black', 'green', 'white', 'blue', 'blue', 'yellow', 'red', 'red',
 'black', 'purple', 'blue', 'yellow', 'yellow', 'green', 'orange', 'purple', 're
d', 'orange', 'red', 'blue', 'green', 'purple', 'orange', 'red', 'white', 'orang
e', 'green', 'purple', 'white', 'green', 'green']
或者,您甚至可以在
lambda
中省略

>>> map(lambda: random.choice(c), range(50))

所有版本中的
表示未使用的变量。它可以是任何东西,比如
i
number
。例如,在您的情况下,您永远不会真正想要使用
range
函数生成的数字。这是一个惯例。

你可以得到50种颜色,如下所示:

>>> c = ["red", "blue", "green", "yellow", "purple", "orange", "white", "black"]
>>> for _ in range(50):
...     random.choice(c)
...     
... 
'blue'
'red'
'yellow'
...
要生成50种颜色的列表,请使用列表:

>>> colors = [random.choice(c) for _ in range(50)]
>>> colors
['green', 'red', 'green', 'orange', 'orange', 'black', 'black', 'purple', 'red',
 'black', 'yellow', 'yellow', 'white', 'purple', 'orange', 'white', 'red', 'gree
n', 'black', 'blue', 'orange', 'green', 'orange', 'blue', 'white', 'blue', 'oran
ge', 'black', 'orange', 'green', 'black', 'green', 'blue', 'orange', 'blue', 'gr
een', 'green', 'white', 'red', 'green', 'yellow', 'yellow', 'blue', 'orange', 'p
urple', 'green', 'black', 'yellow', 'black', 'red']
您也可以像这样使用
map

>>> map(lambda _: random.choice(c), range(50))
['purple', 'yellow', 'black', 'white', 'black', 'black', 'purple', 'red', 'blue'
, 'white', 'blue', 'white', 'yellow', 'yellow', 'white', 'blue', 'orange', 'whit
e', 'yellow', 'black', 'green', 'white', 'blue', 'blue', 'yellow', 'red', 'red',
 'black', 'purple', 'blue', 'yellow', 'yellow', 'green', 'orange', 'purple', 're
d', 'orange', 'red', 'blue', 'green', 'purple', 'orange', 'red', 'white', 'orang
e', 'green', 'purple', 'white', 'green', 'green']
或者,您甚至可以在
lambda
中省略

>>> map(lambda: random.choice(c), range(50))

所有版本中的
表示未使用的变量。它可以是任何东西,比如
i
number
。例如,在您的情况下,您永远不会真正想要使用
range
函数生成的数字。这是一种约定。

听起来像是要为
映射提供一个函数,该函数放弃给定给它的参数,并生成一个完全独立的值。允许lambda包含表达式体中从未使用过的参数。你不必试图用(x-x)欺骗解释器


听起来您想为
map
提供一个函数,该函数放弃给定给它的参数并生成一个完全独立的值。允许lambda包含表达式体中从未使用过的参数。你不必试图用(x-x)欺骗解释器


您能提供一些示例输出吗?您能提供一些示例输出吗?
\uuu
只是迭代器中游标的变量名。使用
\uu
是一个变量的惯例,该变量没有实际使用,但在语法上是必需的。我已经编辑了对答案中注释的响应。+1列表理解看起来更容易,但赋值规范使用map()@dhali在那里添加了一个
映射
,仅用于记录。
\uu
只是迭代器中光标的变量名。使用
\uu
是一个变量的惯例,该变量没有实际使用,但在语法上是必需的。我已经编辑了对答案中注释的响应。+1列表理解看起来更容易,但赋值规范使用map()@dhali在那里添加了一个
map
,只是为了记录。在作业规范中,虽然列表理解看起来更容易,但它确实声明使用map。在作业规范中,它确实声明使用map,尽管列表理解看起来更容易。