遍历 python 列表和字符串格式
looping over python list and string formating
以字符串格式列表循环列表
我有以下变量
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"
print(o)
有效,如果我在格式字符串中省略 OS,我将得到值(Linux
、Unix
、Window
) .
I am installing in {}, side {} using the {} cd".format (BUILDING,SIDE)
但是格式化列表不接受 o 变量,我得到的错误是:
NameError: name 'o' is not defined.
感谢帮助。
我已将 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
查看实际效果 here
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)
尝试创建一个将 o 作为参数的函数 FLIST
:
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)
以字符串格式列表循环列表
我有以下变量
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"
print(o)
有效,如果我在格式字符串中省略 OS,我将得到值(Linux
、Unix
、Window
) .
I am installing in {}, side {} using the {} cd".format (BUILDING,SIDE)
但是格式化列表不接受 o 变量,我得到的错误是:
NameError: name 'o' is not defined.
感谢帮助。
我已将 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
查看实际效果 here
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)
尝试创建一个将 o 作为参数的函数 FLIST
:
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)