Iterating through a list and querying a value

Beautiful Music

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"

PlopMon

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Iterating through a list of a class

Iterating through subelements of a list

Django template not iterating through list

How to iterate through a list and while iterating, iterate through another list and replace values for upper list with inner list key value pairs

Iterating through a string in C

Iterating through two arrays

PHP iterating through arrays

Iterating through a firebase collection

Iterating through SQL results in Java

iterating through object of objects with condition

Iterating Through multiple dropdowns in jquery

Querying for a list of top entries and their values

Iterating through object with different defined patterns

Speeding up iterating through two foreach loops

Iterating through key names from a PSCustomObject

Iterating through multiple levels of Child Objects

Improving the efficiency of iterating through data frames

PHP iterating through a count shows incorrect sequence

For loop iterating through two lists python

Iterating a generic list with super keyword

Python - Error , iterating list of dictionaries

Is it correct to append to a list while iterating over it?

Clojure Iterating over a list of hash maps

Iterating through a JSON object and display results in regular intervals

print all the values of a group of objects without iterating through it

Realm Cocoa: skipped item when iterating through RLMArray

C# - Iterating through repeated context menu items with foreach loops

Why is iterating through my pandas data changing the values?

check for multiple substring match in a list while iterating list

TOP 리스트

  1. 1

    Matlab의 반복 Sortino 비율

  2. 2

    ImageJ-히스토그램 빈을 변경할 때 최대, 최소 값이 변경되는 이유는 무엇입니까?

  3. 3

    Excel : 합계가 N보다 크거나 같은 상위 값 찾기

  4. 4

    C #에서 'System.DBNull'형식의 개체를 'System.String'형식으로 캐스팅 할 수 없습니다.

  5. 5

    원-사각형 충돌의 충돌 측면을 찾는 문제

  6. 6

    Oracle VirtualBox-설치를 위해 게스트를 부팅 할 때 호스트 시스템이 충돌 함

  7. 7

    어떻게 아무리 "나쁜", ANY의 SSL 인증서와 HttpClient를 사용하지합니다

  8. 8

    Ubuntu는 GUI에서 암호로 사용자를 만듭니다.

  9. 9

    잘못된 상태 예외를 발생시키는 Apache PoolingHttpClientConnectionManager

  10. 10

    Python 사전을 사용하는 동안 "ValueError : could not convert string to float :"발생

  11. 11

    openCV python을 사용하여 텍스트 문서에서 워터 마크를 제거하는 방법은 무엇입니까?

  12. 12

    Vuetify 다중 선택 구성 요소에서 클릭 한 항목의 값 가져 오기

  13. 13

    C ++ VSCode에서 같은 줄에 중괄호 서식 지정

  14. 14

    Cassandra에서 버전이 지정된 계층의 효율적인 모델링

  15. 15

    JQuery datepicker 기능이 인식되지 않거나 새 프로젝트에서 작동하지 않음

  16. 16

    cuda 11.1에서 Pytorch를 사용할 때 PyTorch가 작동하지 않음: Dataloader

  17. 17

    jfreecharts에서 x 및 y 축 선을 조정하는 방법

  18. 18

    상황에 맞는 메뉴 색상

  19. 19

    마우스 휠 JQuery 이벤트 핸들러에 대한 방향 가져 오기

  20. 20

    매개 변수에서 쿼리 객체를 선언하는 방법은 무엇입니까?

  21. 21

    Maven은 아이 프로젝트 대상 폴더를 청소하지

뜨겁다태그

보관