检查2D列表的索引并根据2D列表打印出索引

时间:2018-08-22 10:04:21

标签: python list

我想尝试另一个示例,以打印2D列表的索引。示例:

a = [["text", "man","chest","funny"],["cruel", "just","for","testing"],["I", "take","this","for"],["learning", "purpose","only","please"] ]

b = [["text", "funny"], ["cruel"],["I", "take"], ["for","learning", "purpose"]]

ba = ["text", "funny", "purpose"]

我的代码是这样的:

store_x = []

for x in b:
    for i,xx in enumerate(x):
        if xx in ba:
            store_x.append(i)


print(store_x)

dic2 = []    
for x,y in zip(store_x,a):
    result = []
    for u in str(x):
        dic2.append(y[int(u)])

print(dic2)

当前输出:

[0, 1, 2]
['text', 'just', 'this']

预期输出:

[0][0,1],[3][2] # {b} based on {ba} Not sure whether this is how the index #should be look like for 2D
[["text","man"],["only"]] # store_x based on {a}

我想首先基于{ba}找到{b}的索引值,然后从该索引值中,通过使用存储在{store_x}中的索引,将其用于从{a}打印该值

新功能:

当我们处理{ba}元素时,这是另一个问题。例如

a = [["text", "funny"], ["cruel"],["I", "take"], ["for","learning", "purpose"]]
b = [["text", "funny"], ["cruel"],["I", "take"], ["for","learning", "purpose"]]

ba = ["funny","text", "take","new","I", "purpose", "learning", "cruel"]

然后我当前使用的代码是:

store_x = {}

for ex, x in enumerate(b):
    row = []
    for i, xx in enumerate(x):
        if xx in ba:
            row.append(i)
    if row:
        store_x[ex] = row

print(store_x)

order = {e: ii for ii, e in enumerate(ba)}

dic2 = []
for i, x in store_x.items():
    row = []
    if x:
        for ex in x:
            dic2.append(a[i][ex])

    if dic2:
        dic2.sort(key=lambda e: order.get(e, len(ba)))


print(dic2)

store_x值似乎是:

{0: [0, 1], 1: [0], 2: [0, 1], 3: [1, 2]}

不是我所期望的:

{0: [1, 0], 2: [1,0], 3: [2, 1], 1: [0]}

当前dic2 :(这是正确的,但store_x值不是我期望的值)

['funny', 'text', 'take', 'I', 'purpose', 'learning', 'cruel']

更新!

我尝试使用我的新代码:

s = []

for ex, x in enumerate(ba):
    store_x = {}
    for i, xx in enumerate(b):
        if x in xx:

            store_x[i] = [xx.index(x)]
    s.append(store_x)


print(s)
dic2 = []
for x in s:
    for i, xx in x.items():
        row = []
        if xx:
            for u in xx:
                dic2.append(a[i][u])

print(dic2)

输出为:

[{0: [1]}, {0: [0]}, {2: [1]}, {}, {2: [0]}, {3: [2]}, {3: [1]}, {1: [0]}]
['funny', 'text', 'take', 'I', 'purpose', 'learning', 'cruel']

但这仍然不是我期望的输出:

我的预期输出:

{0: [1, 0], 2: [1,0], 3: [2, 1], 1: [0]}

有人可以帮我这个忙吗?

高于全部!

发现了另一个问题。

代码:

a = [["text", "funny"], ["cruel"],["I", "take"], ["for","learning", "purpose"], ["learning", "to", "learning", "when", "I", "have", "time", "for", "learning"]]
b = [["text", "funny"], ["cruel"],["I", "take"], ["for","learning", "purpose"], ["learning", "to", "learning", "when", "I", "have", "time", "for", "learning"]]

ba = ["funny","text","learning", "take","new","I", "purpose", "cruel"]

从Daniel Mesejo(好家伙)那里获得解决方案时。

输出变为:

{0: [1, 0], 1: [0], 2: [1, 0], 3: [1, 2], 4: [0, 2, 7]}
['funny', 'text', 'learning', 'learning', 'learning', 'learning', 'take', 'I', 'purpose', 'cruel']

预期的输出应为:(鉴于“学习”已经出现过一次,因此我们不应该再次包含它)

{0: [1, 0], 1: [0], 2: [1,0], 3: [1,2], 4: [0, 2, 8,4]} <---- (my idea is that since "learning" appears 3 times here, maybe we should take one time only despite which index it is in). With this, it could be 4:[0] or [2] or [8]. 

“ I”出现在2D列表的两个不同索引中。我的想法是采用任一索引,仅返回1次。可能是2:[1,0]或4:[0,4]或4:[2,4]或4:[8,4]

['funny', 'text', 'learning', 'take', 'I', 'purpose', 'cruel']

希望这也可以做到。我仍在尽力了解更多信息。谢谢您一直以来的帮助。

解决了!这里供以后参考:

store_x = {}
vex = []
for ex, x in enumerate(b):
    row = []
    for i, xx in enumerate(x):
        if xx not in vex:
            if xx in ba:          
                row.append(i)
                vex.append(xx)
    if row:
        row.sort(key=lambda e: ba.index(x[e]))
        store_x[ex] = row

print(store_x)

order = {e: ii for ii, e in enumerate(ba)}

dic2 = []
for i, x in store_x.items():
    if x:
        for ex in x:
            dic2.append(a[i][ex])
dic2.sort(key=lambda e: order.get(e, len(ba)))
print(dic2)

1 个答案:

答案 0 :(得分:1)

您可以这样做:

store_x = []
for x in b:
    row = []
    for i, xx in enumerate(x):
        if xx in ba:
            row.append(i)
    store_x.append(row)

print(store_x)

dic2 = []
for i, x in enumerate(store_x):
    row = []
    if x:
        for ex in x:
            row.append(a[i][ex])
    dic2.append(row)

print(dic2)

输出

[[0, 1], [], [], [2]]
[['text', 'man'], [], [], ['only']]

可以读取输出,因为store_x在索引0中具有值[0,1],在索引3中具有值[2]

您的代码中的问题是store_x是一个列表,您需要一个列表列表(二维列表)。一种选择是在第一个循环中使用字典:

store_x = {}
for ex, x in enumerate(b):
    row = []
    for i, xx in enumerate(x):
        if xx in ba:
            row.append(i)
    if row:
        store_x[ex] = row

print(store_x)

dic2 = []
for i, x in store_x.items():
    row = []
    if x:
        for ex in x:
            row.append(a[i][ex])
    if row:
        dic2.append(row)

print(dic2)

输出

{0: [0, 1], 3: [2]}
[['text', 'man'], ['only']]

这更像预期的输出。

更新 如注释中所指定的,假设ba缺少的值必须排在最后,则输出大多数遵循ba的顺序。该代码必须更新为:

store_x = {}
for ex, x in enumerate(b):
    row = []
    for i, xx in enumerate(x):
        if xx in ba:
            row.append(i)
    if row:
        store_x[ex] = row

print(store_x)

order = {e: ii for ii, e in enumerate(ba)}

dic2 = []
for i, x in store_x.items():
    row = []
    if x:
        for ex in x:
            row.append(a[i][ex])

    if row:
        row.sort(key=lambda e: order.get(e, len(ba)))
        dic2.append(row)

print(dic2)

输出(测试用例1)

{0: [0, 1], 3: [2]}
[['text', 'man'], ['only']]

输出(测试用例2)

{0: [0], 2: [1], 3: [1, 2]}
[['text'], ['take'], ['purpose', 'learning']]

更新2

store_x = {}
for ex, x in enumerate(b):
    row = []
    for i, xx in enumerate(x):
        if xx in ba:
            row.append(i)
    if row:
        row.sort(key=lambda e: ba.index(x[e]))
        store_x[ex] = row

print(store_x)

order = {e: ii for ii, e in enumerate(ba)}

dic2 = []
for i, x in store_x.items():
    if x:
        for ex in x:
            dic2.append(a[i][ex])
dic2.sort(key=lambda e: order.get(e, len(ba)))
print(dic2)

输出(测试用例1)

{0: [0, 1], 3: [2]}
['text', 'man', 'only']

输出(测试用例2)

{0: [0], 2: [1], 3: [2, 1]}
['text', 'take', 'purpose', 'learning']

输出(测试用例3)

{0: [1, 0], 1: [0], 2: [1, 0], 3: [2, 1]}
['funny', 'text', 'take', 'I', 'purpose', 'learning', 'cruel']