Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

python - Joining elements in a list without the join command

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...