我有一个要求,我试图找到列表中的值是否在另一个列表中找到,然后单独使用该值。
例如
MyCountryList = ( 'United States', 'United Kingdom', 'Russia')
data = (['United States', 'IDB', 1989, Decimal('275200141')],
['United States', 'IDC', 1990, Decimal('282899816')],
'India', 'IDD', 1989, Decimal('275200141')],
['India', 'IDE', 1990, Decimal('282899816')])
我希望我的输出只有美国,因为这是这个格式的两个列表中唯一没有单词十进制和''在名单上。
MyRequiredList = (['United States', 'IDB', '1989', '275200141'],
['United States', 'IDC', '1990', '282899816'])
data[0] gives me this ['United States', 'IDB', 1989, Decimal('92490932')]
答案 0 :(得分:1)
你可以试试这个:
from decimal import Decimal
MyCountryList = ( 'United States', 'United Kingdom', 'Russia')
data = (['United States', 'IDB', 1989, Decimal('275200141')],
['United States', 'IDC', 1990, Decimal('282899816')],
['India', 'IDD', 1989, Decimal('275200141')],
['India', 'IDE', 1990, Decimal('282899816')])
final_data = [i for i in data if i[0] in MyCountryList]
输出:
[['United States', 'IDB', 1989, Decimal('275200141')], ['United States', 'IDC', 1990, Decimal('282899816')]]