在python列表上循环和字符串格式化
在字符串格式的列表中循环列表 我有以下变量在python列表上循环和字符串格式化,python,string,string-formatting,Python,String,String Formatting,在字符串格式的列表中循环列表 我有以下变量 BUILDING = "123" SIDE = "ProductionA" TODO = "traveling without moving" 我有以下清单 OS = ["Linux", "Unix", "Windows"] 我创建一个格式化的字符串列表 FLIST = [ "I am installing in {}, side {} using the {} cd".format (BUILDING,SIDE,o), "Other random
BUILDING = "123"
SIDE = "ProductionA"
TODO = "traveling without moving"
我有以下清单
OS = ["Linux", "Unix", "Windows"]
我创建一个格式化的字符串列表
FLIST = [
"I am installing in {}, side {} using the {} cd".format (BUILDING,SIDE,o),
"Other random stuff",
"Even more random stuff: ".format(TODO)]
我想循环列表:
for o in OS:
print(o)
for f in FLIST:
print(f)
我希望得到:
"I am installing in 123, side ProductionA using the Linux cd"
"Other random stuff",
"Even more random stuff: traveling without moving"
"I am installing in 123, side ProductionA using the Unix cd"
"Other random stuff",
"Even more random stuff: traveling without moving"
"I am installing in 123, side ProductionA using the Windows cd"
"Other random stuff",
"Even more random stuff: traveling without moving"
打印(o)
有效,如果我在格式字符串中省略操作系统,我将获得值(Linux
,Unix
,窗口
)
I am installing in {}, side {} using the {} cd".format (BUILDING,SIDE)
但是格式化列表不接受o变量,我得到的错误是:
NameError:未定义名称“o”
非常感谢您的帮助。我已将
FLIST
放入循环中。试试看
BUILDING = "123"
SIDE = "ProductionA"
TODO = "traveling without moving"
OS = ["Linux", "Unix", "Windows"]
for o in OS:
print(o)
FLIST = ["I am installing in {}, side {} using the {} cd".format (BUILDING,SIDE,o),"Other random stuff","Even more random stuff: {}".format(TODO)]
for f in FLIST:
print(f)
输出:
Linux
I am installing in 123, side ProductionA using the Linux cd
Other random stuff
Even more random stuff: traveling without moving
Unix
I am installing in 123, side ProductionA using the Unix cd
Other random stuff
Even more random stuff: traveling without moving
Windows
I am installing in 123, side ProductionA using the Windows cd
Other random stuff
Even more random stuff: traveling without moving
在操作中查看它
FLIST
应该是将o
作为输入的函数:
BUILDING = "123"
SIDE = "ProductionA"
TODO = "traveling without moving"
# Note f-strings only work in python 3.6+, revert to .format() if you need to use an older version
def make_flist(operating_system):
return [
f"I am installing in {BUILDING}, side {SIDE} using the {operating_system} cd",
"Other random stuff",
f"Even more random stuff: {TODO}"
]
operating_systems = ["Linux", "Unix", "Windows"]
for operating_system in operating_systems:
print(operating_system)
for statement in make_flist(operating_system):
print(statement)
尝试创建一个函数
FLIST
,该函数以o为参数:
def FLIST(o):
return [
"I am installing in {}, side {} using the {} cd".format (BUILDING,SIDE,o),
"Other random stuff",
"Even more random stuff: ".format(TODO)
]
然后使用此功能:
for o in OS:
print(o)
for f in FLIST(o):
print(f)
FLIST
应该是将o
作为输入的函数。这听起来像是范围界定的问题。FLIST
中的项目不知道o
,因为它在不同的范围内1-FLIST声明应该在for循环中,2-FLIST中的“在第一个格式()之后应该在FLIST
中删除,只需将o
替换为”{}“
感谢大家的快速响应,就我而言,@mAhMoUdDaFeR的回答是最好的,因为他的回答保持了我列表中大部分内容的机智。尽管来自Dan和Codelt的其他代码也提供了信息,但是for循环中没有定义o
。