作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我试图制作一个包含我需要的所有元素的大列表,如以下代码的输出所示:
set = [1, 5, 15, 30, 60, 240, 360, 720, 1440, 10080]
ShortEMASet = []
LongEMASet = []
MACDSet = []
SignalSet = []
SetofSets = [ShortEMASet, LongEMASet, MACDSet, SignalSet]
for x in set:
ShortEMASet.append("ShortEMA" + str(x))
LongEMASet.append("LongEMASet" + str(x))
MACDSet.append("MACDSet" + str(x))
SignalSet.append("SignalSet" + str(x))
SetofSets
这将返回输出
[['ShortEMA1',
'ShortEMA5',
'ShortEMA15',
'ShortEMA30',
'ShortEMA60',
'ShortEMA240',
'ShortEMA360',
'ShortEMA720',
'ShortEMA1440',
'ShortEMA10080'],
['LongEMASet1',
'LongEMASet5',
'LongEMASet15',
'LongEMASet30',
'LongEMASet60',
'LongEMASet240',
'LongEMASet360',
'LongEMASet720',
'LongEMASet1440',
'LongEMASet10080'],
['MACDSet1',
'MACDSet5',
'MACDSet15',
'MACDSet30',
'MACDSet60',
'MACDSet240',
'MACDSet360',
'MACDSet720',
'MACDSet1440',
'MACDSet10080'],
['SignalSet1',
'SignalSet5',
'SignalSet15',
'SignalSet30',
'SignalSet60',
'SignalSet240',
'SignalSet360',
'SignalSet720',
'SignalSet1440',
'SignalSet10080']]
但问题是,如果我的变量 SetofSets 中包含 100 多个列表,那么将其写出 100 多次会非常乏味,我如何才能使其更具可扩展性?
我尝试了以下方法:
set = [1, 5, 15, 30, 60, 240, 360, 720, 1440, 10080]
ShortEMASet = []
LongEMASet = []
MACDSet = []
SignalSet = []
SetofSets = [ShortEMASet, LongEMASet, MACDSet, SignalSet]
for x in set:
for i in SetofSets:
SetofSets[i].append(str(SetofSets[i]) + str(x))
SetofSets
由于此错误而失败:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-641-81831a6939e2> in <module>
8 for x in set:
9 for i in SetofSets:
---> 10 SetofSets[i].append(str(SetofSets[i]) + str(x))
11 SetofSets
TypeError: list indices must be integers or slices, not list
最佳答案
你可以做嵌套的for循环:
lst = [1, 5, 15, 30, 60, 240, 360, 720, 1440, 10080]
names = ["ShortEMASet", "LongEMASet", "MACDSet", "SignalSet"]
out = []
for n in names:
out.append([])
for l in lst:
out[-1].append(n + str(l))
print(out)
打印:
[
[
"ShortEMASet1",
"ShortEMASet5",
"ShortEMASet15",
"ShortEMASet30",
"ShortEMASet60",
"ShortEMASet240",
"ShortEMASet360",
"ShortEMASet720",
"ShortEMASet1440",
"ShortEMASet10080",
],
[
"LongEMASet1",
"LongEMASet5",
"LongEMASet15",
"LongEMASet30",
"LongEMASet60",
"LongEMASet240",
"LongEMASet360",
"LongEMASet720",
"LongEMASet1440",
"LongEMASet10080",
],
[
"MACDSet1",
"MACDSet5",
"MACDSet15",
"MACDSet30",
"MACDSet60",
"MACDSet240",
"MACDSet360",
"MACDSet720",
"MACDSet1440",
"MACDSet10080",
],
[
"SignalSet1",
"SignalSet5",
"SignalSet15",
"SignalSet30",
"SignalSet60",
"SignalSet240",
"SignalSet360",
"SignalSet720",
"SignalSet1440",
"SignalSet10080",
],
]
或列表理解:
out = [[n + str(l) for l in lst] for n in names]
print(out)
编辑:您可以创建字典而不是列表:
lst = [1, 5, 15, 30, 60, 240, 360, 720, 1440, 10080]
names = ["ShortEMASet", "LongEMASet", "MACDSet", "SignalSet"]
out = {}
for n in names:
for l in lst:
out.setdefault(n, []).append(n + str(l))
print(out)
打印:
{
"ShortEMASet": [
"ShortEMASet1",
"ShortEMASet5",
"ShortEMASet15",
"ShortEMASet30",
"ShortEMASet60",
"ShortEMASet240",
"ShortEMASet360",
"ShortEMASet720",
"ShortEMASet1440",
"ShortEMASet10080",
],
"LongEMASet": [
"LongEMASet1",
"LongEMASet5",
"LongEMASet15",
"LongEMASet30",
"LongEMASet60",
"LongEMASet240",
"LongEMASet360",
"LongEMASet720",
"LongEMASet1440",
"LongEMASet10080",
],
"MACDSet": [
"MACDSet1",
"MACDSet5",
"MACDSet15",
"MACDSet30",
"MACDSet60",
"MACDSet240",
"MACDSet360",
"MACDSet720",
"MACDSet1440",
"MACDSet10080",
],
"SignalSet": [
"SignalSet1",
"SignalSet5",
"SignalSet15",
"SignalSet30",
"SignalSet60",
"SignalSet240",
"SignalSet360",
"SignalSet720",
"SignalSet1440",
"SignalSet10080",
],
}
然后你可以调用:
print(out["ShortEMASet"])
打印:
[
"ShortEMASet1",
"ShortEMASet5",
"ShortEMASet15",
"ShortEMASet30",
"ShortEMASet60",
"ShortEMASet240",
"ShortEMASet360",
"ShortEMASet720",
"ShortEMASet1440",
"ShortEMASet10080",
]
关于python - 我怎样才能使它对大型项目列表更具可扩展性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68229202/
我的按钮有问题。该按钮是登录按钮。当我在输入凭据后单击回车时,将触发查看凭据是否正确的方法。这没有任何问题。 但还应该发生的是,在检查这些凭据是否正确之后,登录和注册按钮应该会被删除,同时会出现一个新
因此,我尝试以 Angular 显示多个 View ,以帮助解决我正在构建的网站中遇到的页脚问题。我想确保我一直在阅读和尝试模仿的内容是有意义的。这就是我到目前为止所拥有的。 index.html
我正在尝试创建可调整大小的 div 容器,而且它们是可拖动的。 我使用了 Angular Material 拖放和 angular resizable element 这是解决方法 https://s
我正在尝试创建可调整大小的 div 容器,而且它们是可拖动的。 我使用了 Angular Material 拖放和 angular resizable element 这是解决方法 https://s
我是一名优秀的程序员,十分优秀!