【问题标题】:How to concatenate 2 lists [closed]如何连接2个列表[关闭]
【发布时间】:2019-10-25 00:37:59
【问题描述】:

list_a : ["http://example.com="]

list_b : [3, 4, 777, 888, 99]

想要的结果:

['http://example.com=3',
'http://example.com=4',
'http://example.com=777',
'http://example.com=888',
'http://example.com=99']

【问题讨论】:

  • 您是否已经尝试过?向我们展示你的尝试,否则你会被否决并且什么也学不到
  • 为什么将地址存储在列表中而不是str
  • 看起来是一个 url 列表 @AdamGold
  • "网址相同"...

标签: python list loops concatenation


【解决方案1】:

编辑

假设您有以下列表:

list_a = ['http://example.com=']
list_b = [3, 4, 777, 888, 99]

你需要做的是创建一个名为list_c的新列表,然后:

for number in list_b:
   list_c.append(list_a[0] + str(number))

【讨论】:

  • 感谢您指出这一点
【解决方案2】:
A="http://example.com="
B=[3, 4, 777, 888, 99]
B=[A+str(i) for i in B]
print(B)

使用python的就地替换技巧改变了B数组。

【讨论】:

  • 嗨,欢迎来到 StackOverflow!你能详细说明你的答案吗?例如,对代码 sn-p 中有趣部分的解释或文档链接?
猜你喜欢
  • 1970-01-01
  • 2019-03-23
  • 1970-01-01
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
相关资源
最近更新 更多