如何遍历列表中的每个字符串?
How do I loop through each string in my list?
我试过这段代码,但它只对列表中的第一个字符串执行函数:
returns 给定字符串列表的第一个和最后两个字符
def both_ends(list):
finalList = []
for s in list:
if s > 2:
return s[0] + s[1] + s[-2] + s[-1]
else:
return s
finalList.append(s)
return finalList
list = ('apple', 'pizza', 'x', 'joke')
print both_ends(string)
如何通过列表中的所有字符串实现此函数 运行?
您想检查字符串的长度,而不是字符串本身。因此,做 s > 2
并没有做你想做的事:
def both_ends(lst):
finalList = []
for s in lst:
if len(s) > 2:
finalList.append(s[0] + s[1] + s[-2] + s[-1])
else:
finalList.append(s)
return finalList
lst = ['apple', 'pizza', 'x', 'joke']
print both_ends(lst)
其他几件事:
- 不要命名变量
list
。它将覆盖内置类型。
- 您有一个元组
(..., ...)
。列表带有方括号。
- 您有
print both_ends(string)
,但没有放入您的列表中。
最后,您可以缩短代码:
print [s[:2] + s[-2:] if len(s) > 2 else s for s in lst]
是的,那是因为您是直接 returning 结果,所以它 returns 在您完成第一个字符串本身之后。相反,您应该将结果放在您创建的 finalList
中,并将结果 return 放在最后。
还有一些其他的东西 -
如另一个答案中所述,您想检查字符串的长度。
字符串的长度应大于 4 ,否则,您最终会多次添加某些字符。
不要对变量使用像 list
这样的名称,它最终会隐藏内置函数,因此您将无法使用 list()
之后创建列表。
最后一个问题是你应该用你的列表调用你的函数,而不是 string
。
例子-
def both_ends(list):
finalList = []
for s in list:
if len(s) > 4:
finalList.append(s[:2] + s[-2:])
else:
finalList.append(s)
return finalList
更简单的方法 -
def both_ends(s):
return s[:2] + s[-2:] if len(s) > 4 else s
lst = ('apple', 'pizza', 'x', 'joke')
print map(both_ends, lst) #You would need `list(map(...))` for Python 3.x
演示 -
>>> def both_ends(s):
... return s[:2] + s[-2:] if len(s) > 4 else s
...
>>> lst = ('apple', 'pizza', 'x', 'joke')
>>> print map(both_ends, lst)
['aple', 'piza', 'x', 'joke']
甚至列表理解,尽管对我来说这会降低它的可读性 -
[s[:2] + s[-2:] if len(s) > 4 else s for s in lst]
演示 -
>>> lst = ('apple', 'pizza', 'x', 'joke')
>>> [s[:2] + s[-2:] if len(s) > 4 else s for s in lst]
['aple', 'piza', 'x', 'joke']
有些问题引起了我的注意。
- 您在第一次迭代后立即 returning,因此仅获取第一个元素。
- 我想你想比较长度是否大于4,所以
len(s)>4
- 不要使用数据类型名称作为变量名称。使用旧的
list
,不要使用那个。
不要立即return,而是附加到列表中。
def both_ends(lst):
finalList = []
for s in lst:
if len(s) > 4:
finalList.append( s[0] + s[1] + s[-2] + s[-1])
else:
finalList.append(s)
return finalList
lst = ['apple', 'pizza', 'x', 'joke']
print both_ends(lst)
输出:
['aple', 'piza', 'x', 'joke']
我试过这段代码,但它只对列表中的第一个字符串执行函数:
returns 给定字符串列表的第一个和最后两个字符
def both_ends(list):
finalList = []
for s in list:
if s > 2:
return s[0] + s[1] + s[-2] + s[-1]
else:
return s
finalList.append(s)
return finalList
list = ('apple', 'pizza', 'x', 'joke')
print both_ends(string)
如何通过列表中的所有字符串实现此函数 运行?
您想检查字符串的长度,而不是字符串本身。因此,做 s > 2
并没有做你想做的事:
def both_ends(lst):
finalList = []
for s in lst:
if len(s) > 2:
finalList.append(s[0] + s[1] + s[-2] + s[-1])
else:
finalList.append(s)
return finalList
lst = ['apple', 'pizza', 'x', 'joke']
print both_ends(lst)
其他几件事:
- 不要命名变量
list
。它将覆盖内置类型。 - 您有一个元组
(..., ...)
。列表带有方括号。 - 您有
print both_ends(string)
,但没有放入您的列表中。
最后,您可以缩短代码:
print [s[:2] + s[-2:] if len(s) > 2 else s for s in lst]
是的,那是因为您是直接 returning 结果,所以它 returns 在您完成第一个字符串本身之后。相反,您应该将结果放在您创建的 finalList
中,并将结果 return 放在最后。
还有一些其他的东西 -
如另一个答案中所述,您想检查字符串的长度。
字符串的长度应大于 4 ,否则,您最终会多次添加某些字符。
不要对变量使用像
list
这样的名称,它最终会隐藏内置函数,因此您将无法使用list()
之后创建列表。最后一个问题是你应该用你的列表调用你的函数,而不是
string
。
例子-
def both_ends(list):
finalList = []
for s in list:
if len(s) > 4:
finalList.append(s[:2] + s[-2:])
else:
finalList.append(s)
return finalList
更简单的方法 -
def both_ends(s):
return s[:2] + s[-2:] if len(s) > 4 else s
lst = ('apple', 'pizza', 'x', 'joke')
print map(both_ends, lst) #You would need `list(map(...))` for Python 3.x
演示 -
>>> def both_ends(s):
... return s[:2] + s[-2:] if len(s) > 4 else s
...
>>> lst = ('apple', 'pizza', 'x', 'joke')
>>> print map(both_ends, lst)
['aple', 'piza', 'x', 'joke']
甚至列表理解,尽管对我来说这会降低它的可读性 -
[s[:2] + s[-2:] if len(s) > 4 else s for s in lst]
演示 -
>>> lst = ('apple', 'pizza', 'x', 'joke')
>>> [s[:2] + s[-2:] if len(s) > 4 else s for s in lst]
['aple', 'piza', 'x', 'joke']
有些问题引起了我的注意。
- 您在第一次迭代后立即 returning,因此仅获取第一个元素。
- 我想你想比较长度是否大于4,所以
len(s)>4
- 不要使用数据类型名称作为变量名称。使用旧的
list
,不要使用那个。
不要立即return,而是附加到列表中。
def both_ends(lst):
finalList = []
for s in lst:
if len(s) > 4:
finalList.append( s[0] + s[1] + s[-2] + s[-1])
else:
finalList.append(s)
return finalList
lst = ['apple', 'pizza', 'x', 'joke']
print both_ends(lst)
输出:
['aple', 'piza', 'x', 'joke']