简体   繁体   中英

How to check if a string matches a value stored in a dictionary list

I have a dictionary where each key has multiple lists as its values. I want to parse a certain index within all the lists to see what value they contain ie I want the 2nd index of the lists. I am trying to check whether or not a certain number(stored as a string) is stored there. If the number is there, I change the bit of a predefined variable from 0 to 1. I am hoping to then store this binary number as the value to the original key in another dictionary.

for key, value in dfDict.items():
  for sublist in value:
    channelBinary = list('0000')
    if '0' in sublist[2]:
        channelBinary[3] = '1'
    if '1' in sublist[2]:
        channelBinary[2] = '1'
    if '2' in sublist[2]:
        channelBinary[1] = '1'
    if '3' in sublist[2]:
        channelBinary[0] = '1'

I know this is wrong because it is going to basically check every list associated with my dictionary key and create a binary number for each list. I only want one binary number which tells me about all the values stored in the 2nd index of every list for each dictionary key. I also tried something like this

for key, value in dfDict.items():
  channelBinary = list('0000')
  if '0' in value[][2]:
      channelBinary[3] = '1'
  if '1' in value[][2]:
      channelBinary[2] = '1'
  if '2' in value[][2]:
      channelBinary[1] = '1'
  if '3' in value[][2]:
      channelBinary[0] = '1'

I know this code is wrong, but hopefully you see that I left out an index for the first square bracket of value which means I essentially want to disregard the list index and only care about the sublist index.

Edit: Sample dictionary structure. This is just the output of printing the dictionary

{'key1': [('data1', 'dataA', '1', 'data_a'),
          ('data2', 'dataB', '2', 'data_b'),
          ('data3', 'dataC', '0', 'data_c')],
 'key2': [('data4', 'dataD', '3', 'data_d'),
          ('data5', 'dataE', '2', 'data_e'),
          ('data6', 'dataF', '1', 'data_f'),]}

Just use your first example but move channelBinary = list('0000') outside the inner loop:

for key, value in dfDict.items():
    channelBinary = list('0000')
    for sublist in value:
        for i in range(len(channelBinary)):
            if str(i) in sublist[2]:
                channelBinary[len(channelBinary) - 1 - i] = '1'

I will add another answer because now for sure I understand what you want, if you want a binary for each key, you can do this, following your code (it could be done a lot simplier and prety, but I don't want to throw out your work here):

dfDict = {'key1': [('data1', 'dataA', '1', 'data_a'),
          ('data2', 'dataB', '2', 'data_b'),
          ('data3', 'dataC', '0', 'data_c'),],
       'key2': [('data4', 'dataD', '3', 'data_d'),
                ('data5', 'dataE', '2', 'data_e'),
                ('data6', 'dataF', '1', 'data_f'),]}
channelBinary = []
index = 0
for key, value in dfDict.items():
    channelBinary.append(list('0000'))
    for sublist in value:
      if '0' in sublist[2]:
          channelBinary[index][3] = '1'
      if '1' in sublist[2]:
          channelBinary[index][2] = '1'
      if '2' in sublist[2]:
          channelBinary[index][1] = '1'
      if '3' in sublist[2]:
          channelBinary[index][0] = '1'
    index += 1


print(channelBinary)

[['0', '1', '1', '1'], ['1', '1', '1', '0']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM