为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

互换的青春 2021-05-30 19:09:09
我想尝试另一个示例,以打印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}打印值。
查看完整描述

1 回答

?
繁花不似锦

TA贡献1851条经验 获得超4个赞

您可以这样做:


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是一个列表,您需要一个列表列表(2D列表)。一种选择是在第一个循环中使用字典:


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']]

这更像是预期的输出。


UPDATE 如注释中所指定,输出最遵循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']


查看完整回答
反对 回复 2021-06-01
  • 1 回答
  • 0 关注
  • 137 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号