Kuracha
Kuracha

Reputation: 321

Looking for integers in dictionaries and lists

I have some txt file which contains combination of lists and dictionaries:

{"e":[[{"e":86,"c":23,"a":
{"a":[120,169,"green","red","orange"],"b":"red"},"g":"yellow","b":
["yellow"],"d":"red","f":-19},{"e":-47,"a":[2],"d":{"a":"violet"}}

I want to find all numbers in this file and get their summary.

I was thinking about maybe iterating over elements by using a for loop and looking for int but it won't work because "for" see elements as all dictionaries or list in this first dictionary and don't go into deeper into dictionaries and lists. I don't want finished program but maybe some clues how I should try to solve this problem.

Upvotes: 0

Views: 98

Answers (1)

Mahmoud Elshahat
Mahmoud Elshahat

Reputation: 1959

you can use regex to look for all numbers and it will be stored in a list then you can carry on:

# read text from file
with open('somefile.txt', 'r') as f:
    text = f.read()

import re
match = re.findall(r'-?\d+', text)
print(match)

output:

['86', '23', '120', '169', '-19', '-47', '2']

explaining regex pattern '-?\d+':

'-?' the text may have a sign or not

'\d+' the text contains any amount of adjacent numbers

Edit:

as mentioned in comments by @arjoonn, the above pattern might catch numbers within the text, so to avoid that we can add a conditions to our regex pattern, as per below example:

import re
text = '2tex809t12 23 [4] -2'
match = re.findall(r'(?<!\w)-?\d+(?!\w)', text)
print(match)

output:

['23', '4', '-2']

explanation: '(?!\w)' called negative lookahead which means the matched text shouldn't contains any characters after our numbers,

and '(?<!\w)' is the same as the above but it serves as negative lookbehind i.e. before

Upvotes: 2

Related Questions