【发布时间】:2020-06-28 18:53:09
【问题描述】:
items = ["Apple", "Banana", "Cherry", "Date", "Eggfruit", "Fig"]
for word in items:
print(word[:3])
#prints the first three characters of each item
def string_k(k, str):
string = []
text = str.split(" ")
for x in text:
if len(x) > k:
string.append(x)
return string
k = 6
str = "Apple, Banana, Cherry, Date, Eggfruit, Fig"
print(string_k(k, str))
#This prints out every item in the list that has more than five characters
这类似于我的第一个问题 - 我有两个单独的代码,但我不明白如何将它们组合在一起以获得我需要的输出
【问题讨论】:
-
print('\n'.join(s[:3] for s in items if len(s)>5)) -
好的,我会试试的:)
标签: python python-3.x string list slice