约 761 字
约 4 分钟
元组、列表、字典和集合,都是可迭代对象,可以通过 for
循环依次读取元素:
元组依次取得元素:
students:tuple[str, str, str] = ("Tom", "Jerry", "Spike")
for student in students:
print(student)
Loading...
列表依次取得元素:
students:list[str] = ["Tom", "Jerry", "Spike"]
for student in students:
print(student)
Loading...
字典依次取得 索引:
score_list:dict[str,int] = {
'Tom': 88,
'Jerry': 99,
'Spike': 66
}
for key in score_list:
print(key, score_list[key])
Loading...
元组依次取得元素:
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pineapple'}
for fruit in fruits:
print(fruit)
Loading...
字符串也是可迭代对象,依次取得字符:
text:str = "hello world"
for word in text:
print(word)
Loading...
创建于 2025/4/11 03:41:55
更新于 2025/4/11 03:41:55