打印和 for 在 1 行 Python 3.6.4

Print and for in 1 line Python 3.6.4

提问人:Simon Van den Bossche 提问时间:3/11/2018 最后编辑:Jon ClementsSimon Van den Bossche 更新时间:3/11/2018 访问量:62

问:

我正在制作护照,它已经完成了。 但是文件有 7kb 大。这对我来说太过分了。 所以我开始把它缩短。我遇到了一个问题,这个(看下面的代码)是它的简化版本,但它不起作用。我可以在打印命令中执行此操作吗?

C = ["A","B","C","D","E","F","G"]
print(C[N] for N in range(0,7))

Questions=["What's your last name?","What's your first name?","On what day are you born? (dd/mm/yyyy + hh:mm)","What's your place of birth?","What's your nationality?","What language(s) do you speak?","What's your sex? (W/M)"]
Data=[input(Questions[N]+"\n") for N in range(0,7)]
Keys = ["Last name","First name","Birthday","Birthplace","Nationality","Language(s)","Sex"]
for N in range(0,7):
    print(repr(N+1)+") "+Keys[N]+": "+Data[N])

(上图)总计 首先,它会问您 7 个问题:(参见“问题”列表)它将答案存储在“数据”列表中,然后告诉您您输入了什么。它应该看起来像这样(下图)

What's your last name?
A
What's your first name?
B
On what day are you born? (dd/mm/yyyy + hh:mm)
C
What's your place of birth?
D
What's your nationality?
E
What language(s) do you speak?
G
What's your sex? (W/M)
H

1) Last name: A
2) First name: B
3) Birthday: C
4) Birthplace: D
5) Nationality: E
6) Language: G
7) Sex: H

问题:我可以在 1 行中执行代码(如下)吗

for N in range(0,7):
    print(repr(N+1)+") "+Keys[N]+": "+Data[N])

`

python-3.x for-循环

评论

1赞 Patrick Haugh 3/11/2018
C[N] for N in range(0,7)是一个生成器表达式。你希望它打印什么?Try 或print(''.join(C))print(*C)
0赞 saud 3/11/2018
给定的描述不是很有帮助。你能更清楚地解释一下你想实现的目标吗?

答: 暂无答案