如果您不理解我的标题,请忽略它,然后阅读本文.... 所以这是我的代码...
alphabets=["a", "b","c", "d","e","f"]
nums=["one","two"]
for num in nums[:2]:
print(num.title())
print(alphabets[:3])
输出就像这样......
one
['a','b','c']
two
['d','e','f']
我不知道该怎么做我已经尝试了很多不同的东西。我只需要将所有代码分成八行,我希望得到类似的输出......
one
['a','b','c']
two
['a','b','c']
唯一的问题是第二行输出相同的东西" a,b和c"相反,我想要" d,e和f"。请帮忙!提前谢谢!
答案 0 :(得分:0)
一个非常简单的解决方案,也许是unpythonic:
alphabets=["a", "b","c", "d","e","f"]
nums=["one","two"]
i = 0
for num in nums[:2]:
print(num.title())
print(alphabets[i:i+3])
i = i + 3
答案 1 :(得分:0)
为尽可能通用,您可以使用字典:
alphabets=["a", "b","c", "d","e","f"]
nums=["one","two"]
converter = {"one":1, "two":2, "three":3, "four":4} #can be expanded upon
new_alpha = {a:b for a, b in zip(nums, [alphabets[i:i+len(nums)+1] for i in range(0, len(alphabets), len(nums)+1)])}
for a, b in sorted(new_alpha.items(), key=lambda x:converter[x[0]]):
print(a)
print(b)
输出:
one
['a', 'b', 'c']
two
['d', 'e', 'f']