在不使用join命令的情况下联接列表中的元素 [英] Joining elements in a list without the join command

查看:75
本文介绍了在不使用join命令的情况下联接列表中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在不使用连接命令的情况下将列表中的元素连接 ,因此,例如,如果我有该列表:

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]

输出应为:

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)

但是,最后,答案最终是125610,这显然是错误的.

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?

推荐答案

如果您只想打印数字而不是return实际的int:

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

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

这篇关于在不使用join命令的情况下联接列表中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆