【问题标题】:creating list of tuples from multiple vectors and constants从多个向量和常量创建元组列表
【发布时间】:2017-09-09 19:06:53
【问题描述】:

我有以下数据:

m = 12
d = 10
ar1 = np.array([1,4,5,6])
type = [p,q,r,s] #same size as ar1

我想创建以下元组列表:

[(12,10,1,p), (12,10,4,q), (12,10,5,r), (12,10,6,s)]

我尝试以各种形式使用zip,但我无法正确使用语法

【问题讨论】:

    标签: python list numpy tuples


    【解决方案1】:

    这应该可以解决问题:

    the_tuple = [ (m, d, n[0], n[1]) for n in zip(ar1,type) ]
    

    【讨论】:

      【解决方案2】:

      使用zip 和列表理解:

      [(m,d,x,y) for x,y in zip(ar1,type)]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-30
        • 2019-11-20
        • 2020-11-26
        • 1970-01-01
        相关资源
        最近更新 更多