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

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

问题描述

我需要加入列表中的元素不使用 join 命令,例如,如果我有列表:

[12,4,15,11]

输出应该是:

1241511

这是我目前的代码:

def 列表(list1):答案 = 0h = len(list1)而 list1 != []:答案 = 答案 + list1[0] * 10 ** hh = h - 1list1.pop(0)打印(答案)

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

我觉得逻辑没问题,但是我找不到问题?

解决方案

如果你只想打印数字而不是返回一个实际的int:

<预><代码>>>>a = [12,4,15,11]>>>打印(*a,sep='')1241511

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?

解决方案

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天全站免登陆