python - 在循环中将多个变量定义为空列表
问题描述
我正在尝试创建和分配 10 个变量,仅通过它们的索引来区分,所有这些变量都是for
循环中的空列表。
理想的输出将是agent_1 = [], agent_2 = [], agent_n = []
我知道我可以全部写出来,但我认为我应该能够创建一个简单的循环。主要问题是在每次迭代中分配空列表
for i in range(1,10):
agent_ + i = []
解决方案
为什么不使用键等于 agent_i 的 dict 对象。
dic = {}
for i in range(1,10):
dic["agent_" + str(i)] = []
// access the dic directly and iterating purpose also just iterate through the dictionary.
print dic["agent_1"]
# iteration over the dictionary
for key,value in dic.items():
print key,value
这是代码片段的链接
推荐阅读
- vb.net - 对DataGridView进行排序时是否可以保留多行的选择?
- c# - 带有 UseOpenIdConnectAuthentication 的 MVC5 ADFS:SecurityTokenReceivedendless 循环
- angular - Angular 8 返回无效的日期
- prestashop-1.7 - 无法让特定主题的模块在 Prestashop 1.7.x 中工作
- css - 点亮元素不显示与 rgba 的边框
- flutter - 等待调试连接的颤振错误:错误状态:无元素
- spring-kafka - kafkatemplate 正在修改发送的消息,需要按原样发送原始消息
- .net - 无法加载项目 Visual Studio 2019
- java - Google Map Directions Api v2 无法执行路线/路径请求
- wordpress - 店面主题 - CSS 到 FrontPage 上的简码