簡體   English   中英

如何在python中使用while循環確定列表中有多少個字符串?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM