[英]How to determine how many strings in a list using while loop in python?
我在下面写了这段代码,我的 while 循环有问题,它遍历列表,但是当它找到除字符串之外的数据类型时,它会中断并打印,我需要它遍历列表的所有元素。
counter = 0
index = 0
a = ['some', 'apple', 'banana', lambda a: a,
'pear', None, 'cherry', """Hello world!""",
'''The Who''', ("a", 5), [("a", "5"), ("b", 3)]]
while type(a[index]) == str:
counter += 1
index += 1
print("There are {} strings in the list".format(counter))
我在下面编写了这段代码,而while循环遇到了问题,它遍历了列表,但是当它找到除中断和打印的字符串以外的数据类型时,我需要它遍历列表的所有元素。
counter = 0
index = 0
a = ['some', 'apple', 'banana', lambda a: a,
'pear', None, 'cherry', """Hello world!""",
'''The Who''', ("a", 5), [("a", "5"), ("b", 3)]]
while type(a[index]) == str:
counter += 1
index += 1
print("There are {} strings in the list".format(counter))
我在下面编写了这段代码,而while循环遇到了问题,它遍历了列表,但是当它找到除中断和打印的字符串以外的数据类型时,我需要它遍历列表的所有元素。
counter = 0
index = 0
a = ['some', 'apple', 'banana', lambda a: a,
'pear', None, 'cherry', """Hello world!""",
'''The Who''', ("a", 5), [("a", "5"), ("b", 3)]]
while type(a[index]) == str:
counter += 1
index += 1
print("There are {} strings in the list".format(counter))
我在下面编写了这段代码,而while循环遇到了问题,它遍历了列表,但是当它找到除中断和打印的字符串以外的数据类型时,我需要它遍历列表的所有元素。
counter = 0
index = 0
a = ['some', 'apple', 'banana', lambda a: a,
'pear', None, 'cherry', """Hello world!""",
'''The Who''', ("a", 5), [("a", "5"), ("b", 3)]]
while type(a[index]) == str:
counter += 1
index += 1
print("There are {} strings in the list".format(counter))
我在下面编写了这段代码,而while循环遇到了问题,它遍历了列表,但是当它找到除中断和打印的字符串以外的数据类型时,我需要它遍历列表的所有元素。
counter = 0
index = 0
a = ['some', 'apple', 'banana', lambda a: a,
'pear', None, 'cherry', """Hello world!""",
'''The Who''', ("a", 5), [("a", "5"), ("b", 3)]]
while type(a[index]) == str:
counter += 1
index += 1
print("There are {} strings in the list".format(counter))
我在下面编写了这段代码,而while循环遇到了问题,它遍历了列表,但是当它找到除中断和打印的字符串以外的数据类型时,我需要它遍历列表的所有元素。
counter = 0
index = 0
a = ['some', 'apple', 'banana', lambda a: a,
'pear', None, 'cherry', """Hello world!""",
'''The Who''', ("a", 5), [("a", "5"), ("b", 3)]]
while type(a[index]) == str:
counter += 1
index += 1
print("There are {} strings in the list".format(counter))
我在下面编写了这段代码,而while循环遇到了问题,它遍历了列表,但是当它找到除中断和打印的字符串以外的数据类型时,我需要它遍历列表的所有元素。
counter = 0
index = 0
a = ['some', 'apple', 'banana', lambda a: a,
'pear', None, 'cherry', """Hello world!""",
'''The Who''', ("a", 5), [("a", "5"), ("b", 3)]]
while type(a[index]) == str:
counter += 1
index += 1
print("There are {} strings in the list".format(counter))
我在下面编写了这段代码,而while循环遇到了问题,它遍历了列表,但是当它找到除中断和打印的字符串以外的数据类型时,我需要它遍历列表的所有元素。
counter = 0
index = 0
a = ['some', 'apple', 'banana', lambda a: a,
'pear', None, 'cherry', """Hello world!""",
'''The Who''', ("a", 5), [("a", "5"), ("b", 3)]]
while type(a[index]) == str:
counter += 1
index += 1
print("There are {} strings in the list".format(counter))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.