'Python: search - in nested list

I want to search '-' in my nested list that contains a nested dictionary. I have also tried the below solution but it didn't work.

List Output:

[[[[50.0, 23.0], [196.0, 23.0], [196.0, 46.0], [50.0, 46.0]], ('주민등록층', 0.8442377)], [[[95.0, 51.0], [154.0, 51.0], [154.0, 65.0], [95.0, 65.0]], ('재외국민', 0.857541)], [[[59.0, 75.0], [118.0, 75.0], [118.0, 95.0], [59.0, 95.0]], ('이영희', 0.92137283)], [[[59.0, 105.0], [197.0, 105.0], [197.0, 121.0], [59.0, 121.0]], ('470315-9428744', 0.96879905)], [[[46.0, 126.0], [144.0, 129.0], [143.0, 146.0], [46.0, 142.0]], ('전라묵도 흥천군', 0.71075964)], [[[48.0, 144.0], [130.0, 144.0], [130.0, 158.0], [48.0, 158.0]], ('각남대요거리는', 0.73041165)], [[[45.0, 160.0], [144.0, 160.0], [144.0, 173.0], [45.0, 173.0]], ('설현말미마를 ', 0.74398714)], [[[132.0, 180.0], [220.0, 179.0], [220.0, 194.0], [132.0, 195.0]], ('-20210908', 0.79617107)], [[[112.0, 199.0], [281.0, 200.0], [281.0, 217.0], [112.0, 216.0]], ('부산특별시 무산구청정', 0.8322405)]]

Code:

var = '-' in (y for x in result for y in x)
        print(var)

Final Output

enter image description here



Solution 1:[1]

This may help

llist = [[[[50.0, 23.0], [196.0, 23.0], [196.0, 46.0], [50.0, 46.0]], ('?????', 0.8442377)], [[[95.0, 51.0], [154.0, 51.0], [154.0, 65.0], [95.0, 65.0]], ('????', 0.857541)], [[[59.0, 75.0], [118.0, 75.0], [118.0, 95.0], [59.0, 95.0]], ('???', 0.92137283)], [[[59.0, 105.0], [197.0, 105.0], [197.0, 121.0], [59.0, 121.0]], ('470315-9428744', 0.96879905)], [[[46.0, 126.0], [144.0, 129.0], [143.0, 146.0], [46.0, 142.0]], ('???? ???', 0.71075964)], [[[48.0, 144.0], [130.0, 144.0], [130.0, 158.0], [48.0, 158.0]], ('???????', 0.73041165)], [[[45.0, 160.0], [144.0, 160.0], [144.0, 173.0], [45.0, 173.0]], ('?????? ', 0.74398714)], [[[132.0, 180.0], [220.0, 179.0], [220.0, 194.0], [132.0, 195.0]], ('-20210908', 0.79617107)], [[[112.0, 199.0], [281.0, 200.0], [281.0, 217.0], [112.0, 216.0]], ('????? ?????', 0.8322405)]]

for x in llist:
    for item1 in x[0]:
        for item1_1 in item1:
            if '-' in str(item1_1):
                print(item1_1)
    for item2 in x[1]:
        if '-' in str(item2):
            print(item2)

It has more simpler logic and it is according to your dataset.

Solution 2:[2]

As you change your question. I change the solution to provide position info.

test_list = [
    [
        [[50.0, 23.0], [196.0, 23.0], [196.0, 46.0], [50.0, 46.0]],
        ("?????", 0.8442377),
    ],
    [
        [[95.0, 51.0], [154.0, 51.0], [154.0, 65.0], [95.0, 65.0]],
        ("????", 0.857541),
    ],
    [
        [[59.0, 75.0], [118.0, 75.0], [118.0, 95.0], [59.0, 95.0]],
        ("???", 0.92137283),
    ],
    [
        [[59.0, 105.0], [197.0, 105.0], [197.0, 121.0], [59.0, 121.0]],
        ("470315-9428744", 0.96879905),
    ],
    [
        [[46.0, 126.0], [144.0, 129.0], [143.0, 146.0], [46.0, 142.0]],
        ("???? ???", 0.71075964),
    ],
    [
        [[48.0, 144.0], [130.0, 144.0], [130.0, 158.0], [48.0, 158.0]],
        ("???????", 0.73041165),
    ],
    [
        [[45.0, 160.0], [144.0, 160.0], [144.0, 173.0], [45.0, 173.0]],
        ("?????? ", 0.74398714),
    ],
    [
        [[132.0, 180.0], [220.0, 179.0], [220.0, 194.0], [132.0, 195.0]],
        ("-20210908", 0.79617107),
    ],
    [
        [[112.0, 199.0], [281.0, 200.0], [281.0, 217.0], [112.0, 216.0]],
        ("????? ?????", 0.8322405),
    ],
]
positions = []
text_infos = []
for line in test_list:
    position = line[0]
    text_info = line[1]
    if "-" in text_info[0]:
        print(f"position: {position}")
        print(f"text_info: {text_info}")
        positions.append(position)
        text_infos.append(text_info)

print(f"positions: {positions}")
print(f"text_infos: {text_infos}")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aadesh Gurav
Solution 2