0

I'm new to Python, sorry for the level of this question.

This is my output (prices from a website). I'm wondering how to convert them into a list of ints

for price_list_items in price_list:
        for values in price_list_items:
            x= values.rstrip(' zł')
            print(x)

479 000
355 000
269 000
499 000
289 000

The desired result will like this [479 000,355 000,... ]. Also, I want to be able to perform basic with the values. I found this thread How to convert a for loop output into a list (python), but it didn't help me.

0

2 Answers 2

1
lista = []

for price_list_items in price_list:
        for values in price_list_items:
            x= values.rstrip(' zł')
            lsita.append(x)

lista = ['479 000', '350 000']
for idx, item in enumerate(lista):
        item = item.split()
        item = ''.join(item)
        lista[idx] = int(item)

print(lista)

~/python/stack$ python3.7 sum.py [479000, 350000]

Change your last line to append to lista instead of print. Now we have lista = ['479 000', ...] but we want ints to perform operations on.

So we can then enumerate our list, from there we can split() and join() to get to here lista = ['479000', ...] then we can just use int(item) and put them back into lista as ints

For fun we could do some map and just go from:

lista = ['479 000', '350 000']
lista = list(map(lambda x: int(''.join((x.split()))), lista))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you vash_the_stampede. It will take some time to understand it but it works fine.
@Paul np I tried to detail it best I could if any questions just ask
0

It looks like your string is meant to be a series of 6-digit numbers but both the individual number-parts, for lack of a better term, are split by spaces and the numbers themselves are split by newlines. The solution, therefore, is to remove the space in between number-parts, converting the result to an integer, like this:

int(part.replace(' ', '')) # Finds all instances of space and replaces them with nothing

Putting this in a list-comprehension, we have:

numbers = [int(l.replace(' ', '')) for l in str]

UPDATE

Since you've posted your code, I can give you a better answer.

[ int(v.rstrip(' zł').replace(' ', '')) for price_list_items in price_list for v in price_list_items ]

8 Comments

If the data was read from a file opened in text mode then line ending will usually get automatically converted to \n; more options are available via the newline arg to open.
@PM2Ring Correct. I wasn't sure how he got his data. It sounded like he already had it in from wherever he got and he was looking to get it into a list of ints from there
Fair enough, but it's probably safe to assume that he just has simple \n line endings. If his multi-line data is in a single string it can be split with .splitlines, which can get rid of the line endings. And if it's already in a list of lines, or is being being read line by line, then the line endings can be removed (along with any other trailing whitespace) by using .rstrip. BTW, your code has both \r\n and \n\r. You should probably fix that. ;)
@PM2Ring question, we can convert these numbers to ints we know, but how is he going to be able to use these ints if we maintain the space between the two sets of 3 digits i.e 430 000 even if its ints shouldn't we eliminate that space
@vash_the_stampede Agreed. That's what my solution does
|

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.