简体   繁体   中英

return first index of element in list

I need to find out if the first character of each name in student_names is in list_char, but I can't figure out how to only use the first index of each name in the if line. I (think? I)need something like charAt from Java but name(0) won't work for me presumably since it's in the for loop?

for name in student_names :
    if name(0) in list_char :
        raumA.append(name)
    else :
        raumB.append(name)
return(raumA, raumB)

Use square brackets for indexing.

for name in student_names :
    if name[0] in list_char :
        raumA.append(name)
    else :
        raumB.append(name)
        return(raumA, raumB)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM