" append 到列表中的每个项目-6ren"> " append 到列表中的每个项目-我在 Python 2.7 中有这段代码,我一直在努力,但我被卡住了。我想从 ["bob","sally","jim"])到 'bob<>sally<>jim' 这是我目前所拥有的。 def long-6ren">
gpt4 book ai didi

python - 将 "<>" append 到列表中的每个项目

转载 作者:行者123 更新时间:2023-11-28 19:55:28 25 4
gpt4 key购买 nike

我在 Python 2.7 中有这段代码,我一直在努力,但我被卡住了。我想从 ["bob","sally","jim"])'bob<>sally<>jim'

这是我目前所拥有的。

def long_winded(my_str):
result = []
for i in my_str:
result += i + "<>"
return result

print long_winded(["sally","bob","jim"])

最佳答案

您可以只使用 .join字符串方法:

my_lst = ['sally', 'bob', 'jim']
result = '<>'.join(my_lst)

当然,您始终可以将所有这些都包装在一个函数中,但您可能不需要:

def long_winded(lst):
return '<>'.join(lst)

print long_winded(['sally', 'bob', 'jim'])

关于python - 将 "<>" append 到列表中的每个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27990383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com