所以我正在努力完成我的标题所建议的。我将它们的名称和乐器存储在列表中。我试图将他们的生日更改为字符串,以便将其与其他两个列表连接起来。

Members = ["Flea", "John Frusciante", "Jack Irons", "Anthony Kiedis"]
Instruments = ["Bassist", "Guitarist", "Drummer", "Musician"]
Birthdates = str([10/16/1962, 3/5/1970, 7/18/1962, 11/1/1962])

New_list = [a + " is the " + b + " and they were born on " + c for a, b, c in zip(Members, Instruments, Birthdates)]
print "\n".join(New_list)


我的结果有点混乱,因为我没有收到任何错误。我希望日期可以打印出来,因为它们记录在“生日”列表中。

Flea is the Bassist and they were born on [
John Frusciante is the Guitarist and they were born on 0
Jack Irons is the Drummer and they were born on ,
Anthony Kiedis is the Musician and they were born on


我知道从那时到现在我缺少一些步骤,但是我的目标看起来像这样:

Flea is the Bassist and they were born on 16 October, 1962.

最佳答案

您不能只输入10/16/1962之类的裸文本。那是一个数学表达式。当Python看到这种情况时,它将立即计算表达式的值,这就是您列表中的内容:

>>> 10/16/1962
0.00031855249745158003


如果要日期,则必须使用date对象:

>>> from datetime import date
>>> date(1962, 10, 16)
datetime.date(1962, 10, 16)
>>> str(date(1962, 10, 16))
'1962-10-16'


如果要将其格式化为16 October, 1962,则必须使用strftime()

>>> date(1962, 10, 16).strftime('%-m %B, %Y')
'10 October, 1962'

关于python - 串联RHCP的名称,工具和生日,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46877806/

10-12 20:10