gpt4 book ai didi

python - 我怎样才能使它对大型项目列表更具可扩展性?

转载 作者:行者123 更新时间:2023-12-01 23:19:59 25 4
gpt4 key购买 nike

所以我试图制作一个包含我需要的所有元素的大列表,如以下代码的输出所示:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com