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
CodePudding user response:
Try this, solution unlimited nesting levels. Item had '-' would be stored in result_items. Besides, I don't know whether number with '-' should be caught. So I also caught numbers with '-' in the solution.
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),
],
]
stack = [test_list]
result_items = []
while stack:
item = stack.pop()
if isinstance(item, list) or isinstance(item, tuple):
for sub_item in item:
stack.append(sub_item)
else:
if "-" in str(item):
result_items.append(item)
print(item)
print(result_items)
CodePudding user response:
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.