14
votes

I need to join the elements in a list without using the join command, so if for example I have the list:

[12,4,15,11]

The output should be:

1241511

Here is my code so far:

def lists(list1):
    answer = 0
    h = len(list1)
    while list1 != []:
        answer = answer + list1[0] * 10 ** h
        h = h - 1
        list1.pop(0)
    print(answer)

But, in the end, the answer ends up being 125610 which is clearly wrong.

I think the logic is OK, but I can't find the problem?

8
I think the logic is flawed, you are always multiplying by 10. That will work only if the number has single digit - thefourtheye
Is there a way to fix it so that numbers with more than 1 digit work? - Twhite1195
Why don't you do string concatenation and finally convert it back to a number? - thefourtheye
My guess is that that would be too close to join(), rather than a numerical solution. - TigerhawkT3
Does it need to print that output, or return the number 1,241,511? - TigerhawkT3

8 Answers

15
votes

If you just want to print the number rather than return an actual int:

>>> a = [12,4,15,11]
>>> print(*a, sep='')
1241511
3
votes

You could just convert each element to a string, add them, and then convert back to an int:

def lists(list1):
    answer=''
    for number in list1:
        answer+=str(number)
    print(int(answer))


lists([12,4,15,11])

>>> 
1241511
3
votes
s = ""
for x in map(str, x):
    s += x

print(s)
1241511
3
votes

There can be few more options like

Option1

>>> lst=[12,4,15,11]
>>> str(lst).translate(None, '[,] ')
'1241511'

Option 2

>>> join = lambda e: str(e[0]) + join(e[1:]) if e else ""
>>> join(lst)
'1241511'

Option 3

>>> ("{}"*len(lst)).format(*lst)
'1241511'

Option 4

>>> reduce(lambda a,b:a+b,map(str,lst))
'1241511'
1
votes

a numeric solution, using your code

import math

def numdig(n):
  #only positive numbers
  if n > 0:
    return int(math.log10(n))+1
  else:
    return 1

def lists(list1):
  answer = 0
  h = 0
  while list1 != []:
    answer = answer * 10 ** h + list1[0]
    list1.pop(0)
    if list1 != []:
      h = numdig(list1[0])
  print(answer)

lists([12,4,15,11])
1
votes

You may try map and reduce with lambda like this:

def without_join(alist):
  try:
    return int(reduce(lambda a,b: a + b, map(str, alist)))
  except ValueError, error:
    print error

  return None


print without_join([12,4,15,11])
1
votes

Here's an entirely numerical solution, playing off of your notion of messing with powers of 10. You were on the right track, but your implementation assumed all values were 1 digit long.

import math

def lists(list1):
    b = 0
    foo = 0
    for item in reversed(list1):
        b += item*(10**foo)
        foo += int(math.floor(math.log10(item))) + 1
    return b

a = [12, 4, 15, 11]
print lists(a)

This returns 1241511, as requested.

All I'm doing here is looping through the list in reverse order and keeping track of how many digits to the left I need to shift each value. This allows integers with an arbitrary number of digits.

-1
votes
list_name_of_program = [a,b,c,d,e,f]
program = ""            
for pro in list_name_of_program:
                program += str(pro)
                program += ","           # you can use seprator a space " " or different 
print(program[:-1])

Output:

'a,b,c,d,e,f'