gpt4 book ai didi

python - 如何将列表中的值输入到字符串中?

转载 作者:太空狗 更新时间:2023-10-30 00:23:53 26 4
gpt4 key购买 nike

我正在尝试将以下列表值输入到下面的 url 字符串中。当我执行以下操作时:

tickers = ['AAPL','YHOO','TSLA','NVDA']
url = 'http://www.zacks.com/stock/quote/{}'.format(tickers)`

Python 返回

http://www.zacks.com/stock/quote/['AAPL', 'YHOO', 'TSLA', 'NVDA']`

我希望它做的是遍历列表并返回以下内容:

http://www.zacks.com/stock/quote/AAPL
http://www.zacks.com/stock/quote/YHOO
http://www.zacks.com/stock/quote/TSLA
http://www.zacks.com/stock/quote/NVDA

谢谢。

最佳答案

map 的绝妙技巧:

url = 'http://www.zacks.com/stock/quote/{}'
tickers = ['AAPL','YHOO','TSLA','NVDA']

list(map(url.format, tickers))

['http://www.zacks.com/stock/quote/AAPL',
'http://www.zacks.com/stock/quote/YHOO',
'http://www.zacks.com/stock/quote/TSLA',
'http://www.zacks.com/stock/quote/NVDA']

关于python - 如何将列表中的值输入到字符串中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48808017/

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