Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在不使用join命令的情况下连接列表中的元素_Python_List_Join - Fatal编程技术网

Python 在不使用join命令的情况下连接列表中的元素

Python 在不使用join命令的情况下连接列表中的元素,python,list,join,Python,List,Join,我需要在不使用join命令的情况下将元素连接到列表中,例如,如果我有列表: [12,4,15,11] 输出应为: 1241511 以下是我目前的代码: def lists(list1): answer = 0 h = len(list1) while list1 != []: answer = answer + list1[0] * 10 ** h h = h - 1 list1.pop(0) print(ans

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

[12,4,15,11]
输出应为:

1241511
以下是我目前的代码:

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,这显然是错误的


我认为逻辑是正确的,但我找不到问题所在?

您可以将每个元素转换为
字符串,添加它们,然后再转换回
int

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


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


您只需将每个元素转换为
字符串
,添加它们,然后再转换回
int

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


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


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

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


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

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

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


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

一个数字解决方案,使用您的代码

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])

一个数字解决方案,使用您的代码

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])

您可以使用
lambda
尝试
map
reduce
,如下所示:

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])

您可以使用
lambda
尝试
map
reduce
,如下所示:

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])

这是一个完全数值化的解决方案,玩弄你的10次方的想法。您的方法是正确的,但是您的实现假设所有值都是1位数

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)
这将根据请求返回
1241511


我在这里所做的就是以相反的顺序循环遍历列表,并跟踪需要向左移动每个值的位数。这允许整数具有任意位数。

这是一个完全数值化的解决方案,它将打破你对10的幂的概念。您的方法是正确的,但是您的实现假设所有值都是1位数

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)
s = ""
for x in map(str, x):
    s += x

print(s)
1241511
这将根据请求返回
1241511


我在这里所做的就是以相反的顺序循环遍历列表,并跟踪需要向左移动每个值的位数。这允许整数具有任意位数。

可以有更多类似的选项

s = ""
for x in map(str, x):
    s += x

print(s)
1241511
选择1 选择2 选择3 选择4
像这样的选择再多也没有了

选择1 选择2 选择3 选择4 输出: 输出:

我认为逻辑是错误的,你总是乘以10。只有当数字只有一个数字时,这才有效。有办法修复它,使超过1位的数字工作吗?为什么不进行字符串连接,最后将其转换回数字?我猜这太接近于
join()
,而不是数字解。它需要打印输出吗,还是返回数字1241511?我认为逻辑有缺陷,你总是乘以10。只有当数字只有一个数字时,这才有效。有办法修复它,使超过1位的数字工作吗?为什么不进行字符串连接,最后将其转换回数字?我猜这太接近于
join()
,而不是数字解。它需要打印输出吗,或者返回数字1241511?哇,我从来没有见过这样的情况:o这只在Python 3上吗?是的,只在Python 3上使用方便的
print()
-as-a-function.:)或者在2.6+Wow中使用来自未来导入打印功能的
。。。但是它是如何工作的呢?为什么在列表变量之前加*呢?“sep”命令的作用是什么?@Twhite1195其名称为Wow,我从未见过这样的情况:o这是否仅在Python3上?是的,仅在Python3上,具有方便的
print()
-as-a-function.:)或者在2.6+Wow中使用来自未来导入打印功能的
。。。但是它是如何工作的呢?为什么在列表变量之前加*呢?“sep”命令的作用是什么?@Twhite1195虽然可以调用,但迭代器变量不要使用相同的变量名。在循环后尝试打印
x
,尽管它可以正常工作,不要对迭代器变量使用相同的变量名。尝试在循环后打印
x
,您可以实际执行
answer=answer*10**h+list1.pop(0)
并删除下一行。您可以实际执行
answer=answer*10**h+list1.pop(0)
并删除下一行。您好,Zeeshan Ahmad!欢迎来到stackoverflow(嗯,我想你已经潜伏了一段时间了)。虽然您的答案在技术上没有使用join就加入了列表中的元素,但是已经有几个答案是正确的,它们通过重复原始海报的代码主题显示了对问题的更好理解,并且对它们为什么这样回答进行了一些描述。您可能会发现阅读或其他帮助文章很有帮助。祝你好运嗨,泽山·艾哈迈德!欢迎来到stackoverflow(嗯,我想你已经潜伏了一段时间了)。虽然您的答案在技术上没有使用join就加入了列表中的元素,但是已经有几个答案是正确的,它们通过重复原始海报的代码主题显示了对问题的更好理解,并且对它们为什么这样回答进行了一些描述。您可能会发现阅读或其他帮助文章很有帮助。祝你好运