Python:如何连接变量和数组

时间:2015-09-24 02:12:00

标签: python

示例I具有以下属性:

a = [1,2,3]
b = [3,2,1]
r1 = 5
r2 = 6

我如何获得:

foo = [1,2,3,3,2,1,5,6]

3 个答案:

答案 0 :(得分:2)

@falsetru 简单如下:

foo = a + b + [r1, r2]

答案 1 :(得分:1)

def combine(*args):
    result = []
    for arg in args:
        if type(arg) is int:
            result.append(arg)
        elif type(arg) is list:
            result += arg
    return result

答案 2 :(得分:1)

您有很多选择:

@falsetru如何说:

foo = a + b + [r1] + [r2]

或者:

foot = []
foot.extend(a)
foot.extend(b)
foot.append(r1)
foot.append(r2)

或者:

foot = []
foot.extend(a)
foot.extend(b)
foot.extend([r1])
foot.extend([r2])

或者:

foot = []
foot.extend(a + b + [r1] + [r2])

您可以在此处了解有关列表的更多信息:Python Data Structures