Home > Net >  How can I list.append("") after looping and one condition was not met?
How can I list.append("") after looping and one condition was not met?

Time:10-06

Create Lists

each list is a different category

a=[]
b=[]
c=[]
d=[]
e=[]
f=[]
words=['a ','b ','c ','d ','e ','f ']
det =['b low','c high','f light','e dark']

I have these letters(words) that i want to remove from det, and after that I want to append each det to the corresponding letter list but if there isn't any correspondence to those words, .append('')

I know i can just put at the end to append '' to the list that don't have elements, but this iteration is for a 'det' that will be changing and appending more and more values to the different lists

Here is the code till now:

i= 0
for x in det:
    if words[0] in x:
        a.append(x.replace(words[0],""))
        i =1
    elif words[1] in x:
        b.append(x.replace(words[1],""))
        i =1
    elif words[2] in x:
        c.append(x.replace(words[2],""))
        i =1
    elif words[3] in x:
        d.append(x.replace(words[3],""))
        i =1
    elif words[4] in x:
        e.append(x.replace(words[4],""))
        i =1
    elif words[5] in x:
        f.append(x.replace(words[5],""))
        i =1
The end result would be something like this after 2 different 'det':

where the second 'det' would be:

det =['a letter','c high','d apple','f light']

end result:

a = ['','letter']
b = ['low','']
c = ['high','high']
d = ['','apple']
e = ['dark','']
f = ['light','light']

CodePudding user response:

Below code should work

words=['a','b','c','d','e','f']
det =['b low','c high','f light','e dark']

data = {letter: [] for letter in words}

for x in det: # for each item in det
    for y in words: # for each item in words
        if x.startswith(y): # if the det starts with letter
            data[y].append(x.replace(y,"").strip()) # data[*letter*] will be appended with *det* item 
        else:
            data[y].append("")
print(data)

this will output (after the 1st det)

[('a', ['', '', '', '']),
 ('b', ['low', '', '', '']),
 ('c', ['', 'high', '', '']),
 ('d', ['', '', '', '']),
 ('e', ['', '', '', 'dark']),
 ('f', ['', '', 'light', ''])])

now let me explain it:

  • Firstly I removed the trailing space from the letters in words
  • then instead of initializing static empty lists, we would initialize a dictionary data with (words) letters as keys, and values of empty lists, this was done using data = {word: [] for word in words} line
  • next we would iterate over both lists to append the list values in the data dictionary.

note that in if x.startswith(y): we used startswith() to ensure the det items starts with the corresponding letter

and in data[y].append(x.replace(y,"").strip()) we used strip() to remove the trailing space after the first letter

CodePudding user response:

Here is one approach. I am using dictionaries to perform the matches and to store the output:

words=['a', 'b', 'c', 'd', 'e', 'f']

det1 = ['b low', 'c high', 'f light', 'e dark']
det2 = ['a letter', 'c high', 'd apple', 'f light']

out = {k: [] for k in words}

# loop over det occurrences
for det in [det1, det2]:
    # create dictionary to match the ids
    # in the form: {'b': 'low', 'c': 'high'...}
    det_dic = {(x:=e.split(' ', 1))[0]: x[1] for e in det}

    # for each key, append match if existing else ''
    for k,v in out.items():
        v.append(det_dic.get(k, ''))

output:

>>> out
{'a': ['', 'letter'],
 'b': ['low', ''],
 'c': ['high', 'high'],
 'd': ['', 'apple'],
 'e': ['dark', ''],
 'f': ['light', 'light']}
  • Related