Python - 如何使用 for 循环累积逗号
Python - how to accumulate comma using a for loop
社区-这是我的第一个post,所以如果我未能正确显示此消息,请原谅我。我正在尝试在测试用例中添加如下所示的逗号。似乎有比我在下面编码的方法更有效的方法;但是,我想使用下面的代码解决问题。我到底错过了什么?
def get_country_codes(prices):
country_prices = prices
p = ""
for i in country_prices:
if i.isalpha() and i == ",":
p = p + i[0] + ","
return (p)
我的代码正在返回:
Test Failed: expected NZ, KR, DK but got
Test Failed: expected US, AU, JP but got
Test Failed: expected AU, NG, MX, BG, ES but got
Test Failed: expected CA but got
from test import testEqual
testEqual(get_country_codes("NZ0, KR00, DK"), "NZ, KR, DK")
testEqual(get_country_codes("US, AU, JP0"), "US, AU, JP")
testEqual(get_country_codes("AU, NG0, MX0, BG0, ES"), "AU, NG, MX, BG, ES")
testEqual(get_country_codes("CA"), "CA")
最好接受 list
而不是 string
类型作为 get_country_codes
的参数。这将使您不必担心解析字符串和忽略逗号。我推荐 Joining Lists and Splitting Strings by diveintopython.net。
此代码接受一个列表,遍历它,拆分 $
上的每个值,获取第一个标记,并检查它是否通过 isalpha()
。如果是,它会附加返回的列表。
def get_country_codes(prices):
"""Get country codes given a list of prices.
:param prices: list of prices
:return: list of alphabetical country codes
"""
countries = []
for price in prices:
country = price.split('$')[0]
if country.isalpha():
countries.append(country)
return countries
# As a proof, this line will concatenate the returned country codes
# with a comma and a space:
print(', '.join(get_country_codes(["NZ0", "KR00", "DK"])))
# As another proof, if your input has to be a string:
print(get_country_codes("US, AU, JP0".split(", ")))
print(get_country_codes(["AU", "NG0", "MX0", "BG0", "ES"]))
print(get_country_codes(["CA"]))
代码returns
NZ, KR, DK
['US', 'AU', 'JP']
['AU', 'NG', 'MX', 'BG', 'ES']
['CA']
最后,做一个断言:
testEqual(get_country_codes(["NZ0", "KR00", "DK"]), ["NZ", "KR", "DK"])
使用您的代码...
对于每个字符检查是否被允许。
在允许的字符中包含“,”和space。
def get_country_codes(prices):
country_prices = prices
p = ""
for i in country_prices:
if i.isalpha() or i == "," or i == " ":
p = p + i[0]
return (p)
社区-这是我的第一个post,所以如果我未能正确显示此消息,请原谅我。我正在尝试在测试用例中添加如下所示的逗号。似乎有比我在下面编码的方法更有效的方法;但是,我想使用下面的代码解决问题。我到底错过了什么?
def get_country_codes(prices):
country_prices = prices
p = ""
for i in country_prices:
if i.isalpha() and i == ",":
p = p + i[0] + ","
return (p)
我的代码正在返回:
Test Failed: expected NZ, KR, DK but got
Test Failed: expected US, AU, JP but got
Test Failed: expected AU, NG, MX, BG, ES but got
Test Failed: expected CA but got
from test import testEqual
testEqual(get_country_codes("NZ0, KR00, DK"), "NZ, KR, DK")
testEqual(get_country_codes("US, AU, JP0"), "US, AU, JP")
testEqual(get_country_codes("AU, NG0, MX0, BG0, ES"), "AU, NG, MX, BG, ES")
testEqual(get_country_codes("CA"), "CA")
最好接受 list
而不是 string
类型作为 get_country_codes
的参数。这将使您不必担心解析字符串和忽略逗号。我推荐 Joining Lists and Splitting Strings by diveintopython.net。
此代码接受一个列表,遍历它,拆分 $
上的每个值,获取第一个标记,并检查它是否通过 isalpha()
。如果是,它会附加返回的列表。
def get_country_codes(prices):
"""Get country codes given a list of prices.
:param prices: list of prices
:return: list of alphabetical country codes
"""
countries = []
for price in prices:
country = price.split('$')[0]
if country.isalpha():
countries.append(country)
return countries
# As a proof, this line will concatenate the returned country codes
# with a comma and a space:
print(', '.join(get_country_codes(["NZ0", "KR00", "DK"])))
# As another proof, if your input has to be a string:
print(get_country_codes("US, AU, JP0".split(", ")))
print(get_country_codes(["AU", "NG0", "MX0", "BG0", "ES"]))
print(get_country_codes(["CA"]))
代码returns
NZ, KR, DK
['US', 'AU', 'JP']
['AU', 'NG', 'MX', 'BG', 'ES']
['CA']
最后,做一个断言:
testEqual(get_country_codes(["NZ0", "KR00", "DK"]), ["NZ", "KR", "DK"])
使用您的代码... 对于每个字符检查是否被允许。 在允许的字符中包含“,”和space。
def get_country_codes(prices):
country_prices = prices
p = ""
for i in country_prices:
if i.isalpha() or i == "," or i == " ":
p = p + i[0]
return (p)