Home > Software engineering >  Create list of arrays that contain specific element
Create list of arrays that contain specific element

Time:05-21

I need to create a function that searches if certain string is within an array and create a list with all the elements (lists) that contain it. For example:

word = 'busy'
array = [[['99', 'Normal', [], ['busy', '0'], 28016.2, 'working', '1']],[['F27', 'Normal', [], ['free', '0'], 28016.537865230806, 'working', '1']]]

So my output should be:

[['99', 'Normal', [], ['busy', '0'], 362.01, 'working', '1']]

But I only get that the validation that says that the string doesn't exist, when it obviously does. Here's the code:

array = [[['99', 'Normal', [], ['busy', '0'], 28016.2, 'working', '1']],[['F27', 'Normal', [], ['free', '0'], 28016.537865230806, 'working', '1']]]

def searchBusyWorkers(array):
    busy = []
    for x in array:
        if 'busy' in x:
            ind = x.index('busy')
            busy.append(array[ind])
            return busy
        else:
            return "No workers have that condition."

CodePudding user response:

'busy' in x fails because "busy" is nested one level deeper. In other words, there is no 'busy' in x but there is a sub-array in x that contains the string "busy".

Since you mention that 'busy' will always be at a particular index, you need to unpack to get the sub-array and then check that index -

for x in array:
    elements, *_ = x
    if 'busy' in elements[3]:
        print(x)

output

[['99', 'Normal', [], ['busy', '0'], 28016.2, 'working', '1']]

CodePudding user response:

Try This

word = 'busy'
array = [[['99', 'Normal', [], ['busy', '0'], 28016.2, 'working', '1']],
[['F27', 'Normal', [], ['free', '0'], 28016.537865230806, 'working', '1']]]


def searchBusyWorkers(array,word):
    out = []
    for a in array:
        for i in a:
            for x in i:
                try:
                    if word in x:
                        out.append(i)
                except TypeError:
                    if word == x:
                        out.append(i)
    if out:
        return out
    return "No workers have that condition."

print(searchBusyWorkers(array,word))

OUTPUT

[['99', 'Normal', [], ['busy', '0'], 28016.2, 'working', '1']]

CodePudding user response:

I would do it this way:

array = [[['99', 'Normal', [], ['busy', '0'], 28016.2, 'working', '1']],[['F27', 'Normal', [], ['free', '0'], 28016.537865230806, 'working', '1']]]

def searchforword(word, array):
    return([y for x in array for y in x for i in y if word in str(i)])

Results:

word = 'busy'
print(searchforword(word, array))
[['99', 'Normal', [], ['busy', '0'], 28016.2, 'working', '1']]
word = 'free'
print(searchforword(word, array))
[['F27', 'Normal', [], ['free', '0'], 28016.537865230806, 'working', '1']]

CodePudding user response:

This should work

new_list = []
for i in range(len(array)):
  for j in (array[i]):
    if isinstance(j, list):
      try:
        for r in j:
          if word in r:
            new_list.append(array[i])
      except:
        ...
    if word in j or word in array[i]: #update
         new_list.append(array[i])

print(*new_list) # Update

Output:

[['99', 'Normal', [], ['busy', '0'], 28016.2, 'working', '1']]
  • Related