Python比较2个列表

时间:2016-09-22 07:27:16

标签: python

我有以下两个列表:

# a unicode list
A= ["[u'899', u'1395']", "[u'908', u'2905']", "[u'423', u'2807']", "[u'440', u'9467']", "[u'430', u'722']", "[u'427', u'1700']", "[u'444', u'1696']", "[u'443', u'2672']", "[u'1049', u'2099']", "[u'916', u'2316']", "[u'923', u'921']", "[u'905', u'1172']", "[u'1025', u'786']", "[u'433', u'896']", "[u'426', u'1628']", "[u'961', u'732']", "[u'922', u'944']", "[u'434', u'11981']", "[u'1058', u'1429']", "[u'1056', u'1761']", "[u'896', u'1548']", "[u'432', u'3015']", "[u'974', u'805']", "[u'1091', u'2654']", "[u'1098', u'212']", "[u'976', u'694']", "[u'949', u'742']", "[u'1048', u'752']", "[u'900', u'1574']", "[u'852', u'668']", "[u'466', u'1545']", "[u'925', u'1030']", "[u'435', u'1298']", "[u'1064', u'853']", "[u'431', u'2879']"]

# Type list
B= [[423L, '$2,779'], [426L, '$1,628'], [427L, '$1,664'], [430L, '$655'], [431L, '$2,658'], [432L, '$3,015'], [433L, '$896'], [434L, '$11,981'], [435L, '$1,298'], [440L, '$9,467'], [443L, '$2,672'], [444L, '$1,696'], [466L, '$1,545'], [787L, '$804'], [852L, '$664'], [896L, '$1,548'], [899L, '$1,395'], [900L, '$1,574'], [905L, '$1,172'], [908L, '$2,886'], [916L, '$2,286'], [922L, '$944'], [923L, '$921'], [925L, '$875'], [934L, '$2,575'], [949L, '$732'], [961L, '$727'], [974L, '$802'], [976L, '$511'], [1025L, '$786'], [1048L, '$752'], [1049L, '$2,099'], [1056L, '$1,761'], [1058L, '$1,417'], [1064L, '$835'], [1072L, '$3,409'], [1091L, '$2,654'], [1098L, '$212']]

我想采取第一个项目列表A"[u'899', u'1395']"并在第二个列表中搜索它。如果找到,则比较值并打印如果它们是相同的或不同的,并且对于列表中的所有其他项目是相同的。 有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

我认为最好的解决方案是从第二个列表中创建一个字典,并使用第一个列表来比较这些项目。

第1步 - 将2D类型列表转换为字典

def convert_to_dict(input_list):
    output_dict = {}
    for item in input_list:
        type, value = item
        output_dict[str(type)] = value.strip("$")
    return output_dict

第2步 - 将unicode列表转换为2D(不需要但很适合操作,如果有的话,稍后再添加)

def convert_str_list_to_2d_list(input_list):
    output_list = []
    for item in input_list:
        output_list.append(item.strip("[]u'").split("', u'")) 
        # I would not suggest the above as this is only particular for your scenario with unicode
        # It might be better to use ast.literal_eval; based on requirement
    return output_list

第3步 - 查找并显示项目

def get_item_value_map(unicode_list, type_list):
    types_dict = convert_to_dict(type_list)
    items_list = convert_str_list_to_2d_list(unicode_list)
    for item, value in items_list:
        if item in types_dict and value == types_dict[item]:
            print "Values for %s are identical" % (item)
        else:
            print "Values for %s are different" % (item)

答案 1 :(得分:0)

快速而肮脏的解决方案。请记住,using eval is a bad practice

我还假设,如果您将列表u'1395'中的A与列表'$1,395'中的B进行比较,那么您希望它们相等。

for sublist_A in A:
    x, y = map(int, eval(sublist_A))
    for sublist_B in B:
        if sublist_B[0] == x:
            B_y = int(sublist_B[1].replace('$', '').replace(',', ''))
            print "For {}: {} is {}equal to {}".format(x, B_y, "not " if B_y != y else "", y)

这给出了输出:

For 899: 1395 is equal to 1395
For 908: 2886 is not equal to 2905
For 423: 2779 is not equal to 2807
For 440: 9467 is equal to 9467
For 430: 655 is not equal to 722
For 427: 1664 is not equal to 1700
For 444: 1696 is equal to 1696
For 443: 2672 is equal to 2672
For 1049: 2099 is equal to 2099
For 916: 2286 is not equal to 2316
For 923: 921 is equal to 921
For 905: 1172 is equal to 1172
For 1025: 786 is equal to 786
For 433: 896 is equal to 896
For 426: 1628 is equal to 1628
For 961: 727 is not equal to 732
For 922: 944 is equal to 944
For 434: 11981 is equal to 11981
For 1058: 1417 is not equal to 1429
For 1056: 1761 is equal to 1761
For 896: 1548 is equal to 1548
For 432: 3015 is equal to 3015
For 974: 802 is not equal to 805
For 1091: 2654 is equal to 2654
For 1098: 212 is equal to 212
For 976: 511 is not equal to 694
For 949: 732 is not equal to 742
For 1048: 752 is equal to 752
For 900: 1574 is equal to 1574
For 852: 664 is not equal to 668
For 466: 1545 is equal to 1545
For 925: 875 is not equal to 1030
For 435: 1298 is equal to 1298
For 1064: 835 is not equal to 853
For 431: 2658 is not equal to 2879