Suppose you have a list of teams keys name and type as shown below
teams = [
{"Name": "Manchester",
"Type": "Soccer"
},
{
"Name": "Chelsea",
"Type": "Soccer"
},
{
"Name": "Lakers",
"Type": "Basketball"
}
]
teamStr = ""
#iterate through the list and concatenate to teamStr the team whose type is Soccer
for index in teams:
for value in teams[index].items:
if value == "Soccer":
teamStr += value
If say you wanted to iterate through the list and get the names of Soccer teams only. For example I want to get Chelsea and Manchester as output, In a nutshell I'd like to iterate through the list and concatenate to teamStr the team whose type is Soccer, such that the final teamStr = "Manchester, Chelsea"
You can iterate through the list by calling Type
keys in every iteration
for i in range(len(teams)):
if teams[i]['Types'] == 'Soccer':
teamStr += teams[i]['Name']
I haven't put spaces between team names, but if you want that:
for i in range(len(teams)):
if teams[i]['Types'] == 'Soccer':
teamStr += teams[i]['Name'] + ' '
teamStr.rstrip(' ') #To remove the unwanted whitespace at the end
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다