有什么方法可以使这个 python for 循环更简单吗?
Is there any possible way to make this python for loop more simple?
我对编程还很陌生,我正在通过 Python 速成课程学习 python。我想知道有没有办法让打印语句更简单更短?
father = {'age': 26, 'skin': 'dark', 'complexity': 'tall'}
mother = {'age': 30, 'skin': 'white', 'complexity': 'small'}
sister = {'age': 15, 'skin': 'white', 'complexity': 'tall'}
people = [father, mother, sister] # stored 3 dictionaries inside a list
for character in range(1): # loop the list in range function, loop once
print(f"\nMy fathers age is: {father['age']}, skin is: {father['skin']}, complexity: {father['complexity']}")
print("")
print(f"\nMy mothers age is: {mother['age']}, skin is: {mother['skin']}, complexity: {mother['complexity']}")
print("")
print(f"\nMy sisters age is: {sister['age']}, skin is: {sister['skin']}, complexity: {sister['complexity']}")
如果允许将 people
的类型更改为 dict
,那么您可以这样做:
father = {'age': 26, 'skin': 'dark', 'complexity': 'tall'}
mother = {'age': 30, 'skin': 'white', 'complexity': 'small'}
sister = {'age': 15, 'skin': 'white', 'complexity': 'tall'}
people = {
"father": father,
"mother": mother,
"sister": sister
}
for name in people:
data = ", ".join([f"{k} is: {v}" for k, v in people[name].items()])
print(f"My {name}'s", data)
我们在这里做的是,迭代字典 people
(通过其键为 name
),在 for loop
中我们迭代子字典(父亲,母亲, sister) 并使用 inline for loop
通过它们的键值对制作字符串,最后用 ", "
连接它们并将生成的字符串分配给变量 data
。然后,打印人名和他们的数据
如果不允许更改people
的类型,那么您可以这样做:
people = [father, mother, sister]
names = ["father", "mother", "sister"]
for i in range(len(people)):
data = ", ".join([f"{k} is: {v}" for k, v in people[i].items()])
print(f"My {names[i]}'s", data)
我们只是简单地按列表索引进行迭代
输出将是相同的:
My father's age is: 26, skin is: dark, complexity is: tall
My mother's age is: 30, skin is: white, complexity is: small
My sister's age is: 15, skin is: white, complexity is: tall
我对编程还很陌生,我正在通过 Python 速成课程学习 python。我想知道有没有办法让打印语句更简单更短?
father = {'age': 26, 'skin': 'dark', 'complexity': 'tall'}
mother = {'age': 30, 'skin': 'white', 'complexity': 'small'}
sister = {'age': 15, 'skin': 'white', 'complexity': 'tall'}
people = [father, mother, sister] # stored 3 dictionaries inside a list
for character in range(1): # loop the list in range function, loop once
print(f"\nMy fathers age is: {father['age']}, skin is: {father['skin']}, complexity: {father['complexity']}")
print("")
print(f"\nMy mothers age is: {mother['age']}, skin is: {mother['skin']}, complexity: {mother['complexity']}")
print("")
print(f"\nMy sisters age is: {sister['age']}, skin is: {sister['skin']}, complexity: {sister['complexity']}")
如果允许将 people
的类型更改为 dict
,那么您可以这样做:
father = {'age': 26, 'skin': 'dark', 'complexity': 'tall'}
mother = {'age': 30, 'skin': 'white', 'complexity': 'small'}
sister = {'age': 15, 'skin': 'white', 'complexity': 'tall'}
people = {
"father": father,
"mother": mother,
"sister": sister
}
for name in people:
data = ", ".join([f"{k} is: {v}" for k, v in people[name].items()])
print(f"My {name}'s", data)
我们在这里做的是,迭代字典 people
(通过其键为 name
),在 for loop
中我们迭代子字典(父亲,母亲, sister) 并使用 inline for loop
通过它们的键值对制作字符串,最后用 ", "
连接它们并将生成的字符串分配给变量 data
。然后,打印人名和他们的数据
如果不允许更改people
的类型,那么您可以这样做:
people = [father, mother, sister]
names = ["father", "mother", "sister"]
for i in range(len(people)):
data = ", ".join([f"{k} is: {v}" for k, v in people[i].items()])
print(f"My {names[i]}'s", data)
我们只是简单地按列表索引进行迭代
输出将是相同的:
My father's age is: 26, skin is: dark, complexity is: tall
My mother's age is: 30, skin is: white, complexity is: small
My sister's age is: 15, skin is: white, complexity is: tall