尝试有条件地将元组列表中的元组追加到字典中的元组子列表中

时间:2019-01-20 01:43:23

标签: python list dictionary tuples

我对如何实现自己想要的东西一无所知,所以我在这里发布。

我有一本字典,其中每个值都是一个元组列表(我将其称为dict1),而我将其与一个元组列表进行交叉引用(我将其称为list1) 。想法是将列表1中的任何元组附加到dict1中的每个子列表中(如果子列表中不存在的元组以(tuple,0)的形式)。

是这样的:

dict1 = {2011: [('Debt collection', 5572),
                ('Mortgage', 4483),
                ('Student loan', 431)],
         2012: [('Consumer Loan', 480),
                ('Student loan', 1632),
                ('Medical loan', 1632),
                ('Prepaid card', 83)]}

list1 = [('Debt collection', 5572),
         ('Mortgage', 4483),
         ('Credit reporting', 3230),
         ('Checking or savings account', 2068),
         ('Student loan', 1632),
         ('Credit card or prepaid card', 1577),
         ('Bank account or service', 1327),
         ('Credit card', 1156),
         ('Consumer Loan', 480)]

我希望我的代码检查dict1的每个值列表中是否存在list1中每个元组的第一部分,如果不存在,则以(tuple,0)的形式将其附加到每个值列表中:

dict1 = {2011: [('Debt collection', 5572),
                ('Mortgage', 4483),
                ('Student loan', 431),
                ('Credit reporting', 0),
                ('Checking or savings account', 0),
                ('Credit card or prepaid card', 0),
                ('Bank account or service', 0),
                ('Credit card', 0),
                ('Consumer Loan', 0)],
         2012: [('Consumer Loan', 480),
                ('Student loan', 1632),
                ('Medical loan', 1632),
                ('Prepaid card', 83),
                ('Debt collection', 0),
                ('Mortgage', 0),
                ('Credit reporting', 0),
                ('Checking or savings account', 0),
                ('Credit card or prepaid card', 0),
                ('Bank account or service', 0),
                ('Credit card', 0)]}

我首先尝试将list1中每个元组的第一部分提取为单独的列表:

 available = [tpl[0] for tpl in list1]

然后我尝试以下操作:

 dict1.update(v.extend(tuple((tpl[0],0)) for tpl in v \ 
              for k, v in dict1.items() \
              if tpl[0] not in available))

我收到以下错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-79-53b4b903b6f4> in <module>()
----> 1 dict1.update(v.extend(list(tuple((tpl[0],0)) for tpl in v for k, v in dict1.items()                      
if tpl[0] not in available)))

<ipython-input-79-53b4b903b6f4> in <genexpr>(.0)
----> 1 dict1.update(v.extend(list(tuple((tpl[0],0)) for tpl in v for k, v in dict1.items()                      
if tpl[0] not in available)))

TypeError: 'generator' object is not subscriptable

我一直在寻找,但没有发现任何类似案例。我假设我必须对dict1使用.update,并对每个值列表(v)都使用.extend,以便分别处理每个列表。

2 个答案:

答案 0 :(得分:1)

对于您的问题,字典比元组列表更易于处理。在这种情况下,您可以通过字典理解来合并映射。​​

from operator import itemgetter

# create dictionary with all keys set to 0
allkeys = dict.fromkeys(map(itemgetter(0), list1), 0)

# for each year, combine dictionary mappings
dict1 = {k: {**allkeys, **dict(v)} for k, v in dict1.items()}

如果愿意,可以转换回元组列表:

res = {k: list(v.items()) for k, v in dict1.items()}

嵌套字典选项的结果:

{2011: {'Debt collection': 5572,
  'Mortgage': 4483,
  'Credit reporting': 0,
  'Checking or savings account': 0,
  'Student loan': 431,
  'Credit card or prepaid card': 0,
  'Bank account or service': 0,
  'Credit card': 0,
  'Consumer Loan': 0},
 2012: {'Debt collection': 0,
  'Mortgage': 0,
  'Credit reporting': 0,
  'Checking or savings account': 0,
  'Student loan': 1632,
  'Credit card or prepaid card': 0,
  'Bank account or service': 0,
  'Credit card': 0,
  'Consumer Loan': 480,
  'Medical loan': 1632,
  'Prepaid card': 83}}

答案 1 :(得分:1)

您可以将每个值临时转换为var database = firebase.database().ref().child('transactions'); database.on('value', function(snapshot){ if(snapshot.exists()){ var content = ''; snapshot.forEach(function(data){ var CustomerName = data.val().CustomerName; var AmountSent = data.val().AmountSent; var TimeEffected= data.val().TimeEffected; var DateEffected = data.val().DateEffected; var Successful = data.val().Successful; content += '<tr>'; content += '<td>' + CustomerName + '</td>';//column2 content += '<td>' + AmountSent + '</td>'; //column1 content += '<td>' + TimeEffected + '</td>'; //column1 content += '<td>' + DateEffected + '</td>'; //column1 content += '<td>' + Successful + '</td>'; //column1 content += '</tr>'; }); $('#ex-table').append(content); ,使用dict来确保值的完整补充,并将其写回列表中。这不需要导入:

get

如果要在输出中包含字典,则最后一行几乎没有变化:

for k in dict1:
    d = dict(dict1[k])
    dict1[k] = [(item, d.get(item, 0)) for item, _ in list1]

或者,如果订单很重要,并且您正在使用Python <3.6:

dict1[k] = {item: d.get(item, 0)) for item, _ in list1}

虽然我不建议您这样做,但可能只需要一支班轮:

dict1 = OrderedDict(item, d.get(item, 0)) for item, _ in list1)