0

Here is an example:

x = ["changes": {"Average Response Time": ["22.93705940246582", "21.93705940246582"]}},]

How to extract only floats that I will recieve:

22.93705940246582

21.93705940246582
8
  • 2
    Your syntax is incorrect, it should be x = [{"changes": {"Average Response Time": ["22.93705940246582", "21.93705940246582"]}},]. Commented Jul 19, 2021 at 17:24
  • 1
    Either you have a typo or x is not a valid python object. Commented Jul 19, 2021 at 17:25
  • You can't print floats... Commented Jul 19, 2021 at 17:25
  • 2
    there are no floats in your dictionary. This is crucial to understand. In any case, are you basically asking how to use a dict? Commented Jul 19, 2021 at 17:25
  • 1
    Seems like you are just starting to learn Python. Best thing is to start with some tutorials, rather than asking questions here. For example, find a tutorial on lists and dictionaries which explains how to extract their values (known as 'indexing'). Commented Jul 19, 2021 at 17:26

2 Answers 2

2

It is somehow "possible" but I will recommend you to start learning about Dictionnaries and Python data structure first.

Once you fixed your SyntaxError, assuming you have:

x = [{"changes": {"Average Response Time": ["22.93705940246582", "21.93705940246582"]}},]

You should be able to do:

print("\n\n".join(x[0]["changes"]["Average Response Time"]))

Full output:

>>> x = [{"changes": {"Average Response Time": ["22.93705940246582", "21.93705940246582"]}},]
>>> print("\n\n".join(x[0]["changes"]["Average Response Time"]))
22.93705940246582

21.93705940246582
>>> 

About \n: It's the equivalent of "new line".

About x[0]["changes"]["Average Response Time"]: Your x variable is a list of dictionnaries. To understand let's take it to step by step:

  1. Here we can see that we got the first element of the list which is a dictionary.
>>> x[0]
{'changes': {'Average Response Time': ['22.93705940246582', 
'21.93705940246582']}}
>>> 
  1. We are looking for the "floats" - which are actually strings by the way. Meaning that we first have to "select" the "changes" key. That's what we do here.
x[0]["changes"]
{'Average Response Time': ['22.93705940246582', '21.93705940246582']}
>>>
  1. We now have to "select" the "Average Response Time" key. Same as before.
>>> x[0]["changes"]["Average Response Time"]
['22.93705940246582', '21.93705940246582']
>>> 

About str.join():

Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.


I hope this helps, but please learn about Python lists, dictionaries, and data structure ASAP...

Sign up to request clarification or add additional context in comments.

Comments

1

Your code is wrong since you used "changes": But write as a list, it should be a big dict outside. Your code should look like this:

You can do this:

x = {"changes": {"Average Response Time": ["22.93705940246582", "21.93705940246582"]}}

Then you can do:

print(x['changes']['Average Response Time'])

which gives you:

['22.93705940246582', '21.93705940246582']

If you want the first float:

print(x['changes']['Average Response Time'][0])

Otherwise, if you don't want a dict but want to make it a list. You can use this regular expression which will give you the numbers you want:

regex_digit_match = re.compile(r"(\d+(?:\.\d+)?)")
regex_digit_match.findall(yourlist[index])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.