【问题标题】:Multiplying two lists when the other list contains words as well当另一个列表也包含单词时将两个列表相乘
【发布时间】:2017-08-09 18:45:11
【问题描述】:

如果我有一个列表A 如下:A = [word, 5] 和其他列表B 其中B = [4],我怎么能只乘以数字并得出一个如下列表:new_list = [word, a * b]

【问题讨论】:

  • 如果您有一个包含多个数字的列表会怎样?你能给我们更多的输入/输出示例吗?
  • 你对A = ['word', 5, 'text']B = ['string', 4]有什么期望?
  • 基本上我有一个列表,其中每个索引本身都是一个包含一个单词和一个数字的列表。然后我有一个只包含数字的列表,我想将数字本身相乘。

标签: python list


【解决方案1】:

可能是这样的

for i in a:
    if isinstance(i, int): #Checking if it is an int or not
        for j in b:
            if isinstance(j, int):
               c.append(i*j)
            else:
                if j not in c:
                    c.append(j)
    else:
        c.append(i)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    相关资源
    最近更新 更多