<p>您应该使用<code>enumerate</code>函数迭代<code>func2</code>函数的返回值(<code>tuple</code>)(对<code>d</code>变量也是如此)。枚举函数返回迭代器的值和相关索引(例如:<a href="https://realpython.com/python-enumerate/" rel="nofollow noreferrer">https://realpython.com/python-enumerate/</a>)。然后可以为(空)dict添加元素。应该使用<code>chr</code>函数根据索引定义字母。小写的<code>a</code>是97</p>
<p><strong>相关代码部分:</strong></p>
<pre><code>c = func1()
d = func2()
h = {}
for idx, value in enumerate(d):
h[chr(97 + idx)] = TestClass(c[0], c[1], d[idx])
for key, value in h.items():
with open(f"Name {key}.txt", "w") as f:
f.write(str(value))
</code></pre>
<p><strong>注意:</strong></p>
<p>我已经编写了一个更紧凑的代码版本。如果你对它感兴趣,你可以去看看</p>
<p><strong>代码:</strong></p>
<pre><code>class TestClass(object):
def __init__(self, a, b, c: int):
self.a = a
self.b = b
self.c = c
def __str__(self):
return f" a= {self.a} b = {self.b} c = {self.c}"
a, b, h, d = input("a: "), input("b: "), {}, [100, 90, 80, 70]
result = [(chr(97 + idx), TestClass(a, b, d[idx])) for idx, value in enumerate(d)]
for item in result:
with open(f"Name {item[0]}.txt", "w") as f:
f.write(str(item[1]))
</code></pre>