简体   繁体   中英

Python: List of lists of integers to absolute value to single number

If i had a list of list of integers say:

[['12' '-4' '66' '0'], ['23' '4' '-5' '0'], ['23' '77' '89' '-1' '0']]

I wanted to convert the numbers to their absolute values and then to a single number, so the output would be:

1246602345023778910

What you're showing is (maybe) a list of lists of strings, and the syntax is extremely peculiar -- the sublists are shown with the normal, usual commas, but inside each there are just literal strings with spaces between them. If you actually type that into Python, you'll get a list where each sublist contains a single string -- Python, like C, concatenates at compile time string literals that are simply juxtaposed with whitespace in the middle.

Assuming you did mean to have sublists of several strings each, WITH proper commas, and that your mentions of "lists of numbers" (which is not what you have -- you have lists of strings!-) are just small accidents, something like:

''.join(c for L in thelist for c in L).replace('-', '')

is probably best -- just operate at the string level (so, replace dashes with nothing, rather than using abs ), since you do need strings for concatenation/joining purposes anyway.

If you're keen to do it the most complicated way you can,

''.join(str(abs(int(c))) for L in thelist for c in L)

will also work (and more literally match what you're asking), but the first idea's better.

What you gave above is a list of lists of strings.

I suggest a two-pass approach:

  1. Get integers from strings and take their absolutes:

    [ abs(int(s)) for s in list ]

  2. Combine these numbers into a string and turn it into an integer

    ''.join([ ''.join(x) for x in listoflists ])

When you combine these two approaches you get:

>>> listoflists = [['12','-4','66','0'],['23','4','-5','0'],['23','77','89','-1','0']]
>>> int(''.join([ ''.join([ str(abs(int(s))) for s in list ]) for list in listoflists ]))
1246602345023778910L

It's not nice and readable though, so you may want to keep it divided to make more sense to someone who might have the take the pain of understanding it.

Note: If you indeed had a list of integers though, as you stated, then it's much easier:

>>> listofints = [12,-4,66,0,23,4,-5,0,23,77,89,-1,0]
>>> int(''.join( [ str(abs(x)) for x in listofints ]))
1246602345023778910L

You can use itertools.chain here to get rid of the nesting, concatenate the number-strings, remove the '-' signs and then turn them into a number.

import itertools
mylist = [['12' '-4' '66' '0'], ['23' '4' '-5' '0'], ['23' '77' '89' '-1' '0']]
num = int( ''.join(itertools.chain(*mylist)).replace('-','') )

Edit: I previously missed the abs requirement.
Second edit: Used replace which is probably more efficient than str(abs(int(n))) and also less clumsy (courtesy: Alex's answer)

I am new to Python, so please forgive me if there is a more efficient way :)

#!/usr/bin/python

str = ''
lists = [['12' '-4' '66' '0'], ['23' '4' '-5' '0'], ['23' '77' '89' '-1' '0']]

for list in lists:
  str += list[0].replace('-', '')

print int(str)

As pointed out in the other answers, your inner lists are not as 'listy' as they appear. Since commas are missing, Python will (I think) concatenate them all into one string thus giving you a list of one item. This will be the same as this:

[['12-4660'], ['234-50'], ['237789-10']]

In any case, you could just strip out eveything that's not a digit to get your result (this would work with or without the commas):

''.join(x for x in str(mylist) if x.isdigit())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM