簡體   English   中英

有沒有比使用for循環更簡潔的方法

[英]Is there a cleaner way than using for loops

我想知道是否有任何其他方法來循環和操作位於不同數組中的數據。

import numpy as np

a = np.arange(2)
b = np.arange(5)
c = np.arange(5)

l1 = []
for x in a:
    l2 = []
    for y in b: 
        l3 = []
        y = x + 1  
        for z in c:
            z = x + y
            t = (x,y,z)
            l3.append(t)
        l2.append(l3)
    l1.append(l2) 
print l1

這段代碼正是你正在做的。

def method(lst, range1, range2):
    for i in lst:
        yield [[(i, i+1, 1+(i*2))]*range2]*range1

甚至可以變成生成器表達式:

def gen_method(lst, r1, r2):
    return ([[(i, i+1, 1+(i*2))]*r2]*r1 for i in lst)

如果你願意,可以自己測試。


我的測試:

a = range(2)
b = range(5)
c = range(5)

def your_method(a, b, c):
    l1 = []
    for x in a:
        l2 = []
        for y in b: 
            l3 = []
            y = x + 1  
            for z in c:
                z = x + y
                t = (x,y,z)
                l3.append(t)
            l2.append(l3)
        l1.append(l2)
    return l1

def my_method(lst, range1, range2):
    for i in lst:
        yield [[(i, i+1, 1+(i*2))]*range2]*range1

yours = your_method(a, b, c)
mine = list(my_method(a, len(b), len(c)))

print yours
print '==='
print mine
print '==='
print yours == mine

>>> 
[[[(0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1)], [(0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1)], [(0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1)], [(0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1)], [(0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1)]], [[(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)], [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)], [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)], [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)], [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)]]]
===
[[[(0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1)], [(0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1)], [(0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1)], [(0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1)], [(0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1), (0, 1, 1)]], [[(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)], [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)], [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)], [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)], [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)]]]
===
True

好吧,你可以使用列表推導來壓縮代碼:

[[[(x,x+1,x*2 +1)]*len(c)]*len(b) for x in a]

這樣做是為a中的所有x循環,並創建一個元素列表,其中每個元素是為b中的所有y生成的列表,其中列表的每個元素是(x,x+1,2*x+1)對於c中的所有z。

另一種方法是使用itertools ,將結果列表轉換為numpy array並重新整形

import numpy as np
import itertools as it

a = np.arange(2)
b = np.arange(5)
c = np.arange(5)

l1 = np.array([(i, i+1, i*2+1) for i, rb, rb in it.product(a, b, c)])
l1 = l1.reshape(len(a), len(b), len(c), len(d[0]))

隨着大小的增加,這可能會比其他所有方法消耗更多內存,但它 只有兩行並 為每個三元組創建唯一元素,而不是僅創建指向同一對象的多個指針。

編輯

另一種方式,也允許將三元組(i,i + 1,i * 1 + 1)保持為列表:

l1 = np.empty([len(a), len(b), len(c)], dtype=object)
for i, rb, rb in it.product(a, b, c):
    l1[i,ra, rb] = (i, i+1, i*2+1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM