2016-09-22 59 views
-2

我有以下2所列出:Python的比較2所列出

# 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']"和搜索它在第二個列表。如果找到,則比較這些值並打印如果它們是相同或不同的,並且對列表中的所有其他項目都是相同的。 有人可以幫我嗎?

+0

就目前來看,這個問題還不清楚。你可以給一些小的列表,這些列表仍然足以證明你的問題,然後讓我們知道你希望你的程序給出什麼答案,以及你目前的程序給你什麼? – ymbirtt

+0

這是你想要的,但是你試過了什麼? –

+0

好的。例如我有這個列表[「[u'899',u'1395']」]這是一個unicode列表和這個列表[[899L,'$ 2,779']],它是一個類型列表。我想遍歷第一個列表並查看If 899是否在另一個列表中。如果它確實比較了899的值(在這種情況下是1395)和另一個列表中的值,並打印它們是否相同。 – Marcio125

回答

0

一個快速和骯髒的解決方案。請記住,那using eval is a bad practice

我還假設,如果從列表A'$1,395'從列表B比較u'1395',你想他們是平等的。

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 
+0

而不是eval,你可以用'ast.literal_eval' – thiruvenkadam

1

最好的解決辦法,我想,就是讓一個字典出第二個列表,並使用第一個列表進行比較的品種。

STEP 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 

STEP 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 

STEP 3 - 找到並顯示該項目