【问题标题】:How to fix TypeError can only concatenate list (not "str") to list如何修复 TypeError 只能将列表(不是“str”)连接到列表
【发布时间】:2020-03-22 03:46:05
【问题描述】:

嗨,我是练习列表,我得到 TypeError: can only concatenate list (not "str") to list

如何解决?

我的代码

from bs4 import BeautifulSoup
tag_list= ['Flying','Flight']

links = ['https://goo.gl/'+x for x in tag_list+'/text']
print links

它会给我错误,但如果我将代码编辑为此,它会起作用。

tag_list= ['Flying','Flight']

links = ['https://goo.gl/'+x for x in tag_list]
print links

请问如何解决links = ['https://goo.gl/'+x for x in tag_list+'/text']

【问题讨论】:

  • 期望的输出是什么?
  • 你想通过tag_list + 'text'得到什么? ['Flyingtext','Flighttext'] ?
  • 您可以连接两个字符串或两个列表,即。 tag_list + ['text']
  • 我想要输出是 ['goo.gl/Flying/text'] 因为我什么 res = requests.get(links)
  • 你可以做['https://goo.gl/{}/text'.format(x) for x in tag_list]

标签: python python-2.7


【解决方案1】:

此错误是因为您无法使用运算符+ 添加字符串和列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 2020-06-10
    • 2020-10-20
    • 2020-06-02
    • 2021-02-14
    相关资源
    最近更新 更多