我正在尝试用python创建字典。但是,我想使用随机值创建字典。所需的输出格式应为{'a':1,'b':2}。
import numpy as np
import pandas as pd
import random
import string
value = np.random.random(10)
np.random.seed(42)
key = list(np.random.choice(list(string.ascii_lowercase), 10))
key, value
def map_values(key, value):
i = len(key)
j = len(value)
for i in len(key):
print('key[i]' +'value[j]')
i = i+1
j = j+1
return map_values()
map_values(key, value)
但是在运行上面的代码时,我得到了错误“ int”对象不可迭代的信息。有人可以解释错误的原因并纠正代码,因为我是python新手并尝试学习它。
答案 0 :(得分:2)
您的代码有几个错误:
providers
,而不是整数CREATE TABLE [dbo].[Table](
[B1] [varchar](10) NOT NULL,
[L1] [varchar](5) NOT NULL,
[L2] [varchar](5) NULL,
[E1] [varchar](7) NOT NULL,
[D1] [varchar](50) NULL,
[R1] [varchar](max) NULL,
[Z1] [varchar](5) NULL,
[D2] [varchar](5) NULL,
[Key] AS (([E1]+[B1])+[L1]),
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[Key] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
。range(len(key))
,但不需要递归算法。len(key)
和map_values()
数组的长度,然后在for循环中重新分配它们。key
和value
数组中提取元素。这样,您可以只使用key
+ value
:
dict