Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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 用'连接列表中的项目+';在字符串中签名_Python_List_Python 3.x - Fatal编程技术网

Python 用'连接列表中的项目+';在字符串中签名

Python 用'连接列表中的项目+';在字符串中签名,python,list,python-3.x,Python,List,Python 3.x,我希望我的输出是: Enter a number : n List from zero to your number is : [0,1,2,3, ... , n] 0 + 1 + 2 + 3 + 4 + 5 ... + n = sum(list) 然而,我的实际产出是: Enter a number : 5 List from zero to your number is : [0, 1, 2, 3, 4, 5] [+0+,+ +1+,+ +2+,+ +3+,+ +4+,+ +5+] =

我希望我的输出是:

Enter a number : n
List from zero to your number is : [0,1,2,3, ... , n]
0 + 1 + 2 + 3 + 4 + 5 ... + n = sum(list)
然而,我的实际产出是:

Enter a number : 5
List from zero to your number is :  [0, 1, 2, 3, 4, 5]
[+0+,+ +1+,+ +2+,+ +3+,+ +4+,+ +5+] =  15
我正在使用
join
,因为这是我所知道的唯一类型

为什么在项目周围打印加号,为什么在空白处打印加号

如何将
列表的值打印成字符串供用户阅读

多谢各位。这是我的密码:

##Begin n_nx1 application
n_put = int(input("Choose a number : "))

n_nx1lst = list()
def n_nx1fct():
    for i in range(0,n_put+1):
        n_nx1lst.append(i)
    return n_nx1lst

print ("List is : ", n_nx1fct())
print ('+'.join(str(n_nx1lst)) + " = ", sum(n_nx1lst))

您不需要在列表中调用
str
。返回列表的str表示形式以及与
'+'
联接的列表的输出

您可以使用将列表中的每个项目转换为
str
,然后
join

print('+'.join(map(str, n_nx1lst)) + " = ", sum(n_nx1lst))
您还可以使用新的样式格式来获得更可读的输出:

result = '+'.join(map(str, n_nx1lst))
print("{} = {}".format(result, sum(n_nx1lst)))

列表中的每个
int
元素更改为
中的
str
。使用
生成器表达式连接
调用

print("+".join(str(i) for i in n_nx1lst) + " = ", sum(n_nx1lst))    
在第一种情况下,您调用的是整个
列表中的
str
,而不是该
列表中的单个元素。因此,它将列表表示中的每个字符连接起来,如下所示:

'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]'

使用
+
符号生成您看到的结果。

您需要做的是为列表中的每个元素将字符串元素与“+”连接起来。所有你需要的是有一些字符串格式

def sum_of_input():
    n = int(raw_input("Enter a number : "))  # Get our raw_input -> int
    l = range(n + 1)  # Create our list of range [ x≥0 | x≤10 ]
    print("List from zero to your number: {}".format(l))
    print(' + '.join(str(i) for i in l) + ' = {}'.format(sum(l)))
样本输出:

>>> sum_of_input()
Enter a number : 10
List from zero to your number: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
它是如何工作的?
我们使用所谓的生成器(在这个特定用法中)来迭代
int
元素列表,创建
字符串
元素的
列表。现在我们可以使用
string
方法
join()
创建所需的格式

>>> [str(i) for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
>>> ' + '.join(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'])
'1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10'

当您编写
str(n_nx1lst)
时,您将获得列表的字符串表示形式,因此列表
[1,2,3,4]
从字面上变成字符串
“[1,2,3,4]”
…实际上
”[1,2,3,4]”
,然后联接将字符串作为字符列表,在每个字符之间插入一个
+
。因此,
[+1++、++2+、++3+、++4+]
我现在想补充一点,正确的答案已经公布,您的变量名太糟糕了。。非常混乱且难以阅读。顺便说一句,您不需要编写函数来构建列表。只需执行
n\u nx1lst=list(范围(n\u put+1))
。但是如果你真的想用一个函数来做这件事,它应该把这个数字作为一个参数,它应该在函数中构造一个新的列表并返回它。你说得对。我把清单上的所有东西都变成了一个字符串。Jim向我解释说,我应该遍历每一项。他应该,我将+1,因为它提供了一个很好的alt。只是猜测,但可能投票人不喜欢你向一个甚至不知道如何正确编写简单函数的新手建议
map
。@PM2Ring你有道理。但地图可能和理解一样容易掌握。不过这是主观的,谢谢。我将检查地图功能。谢谢。这就完成了任务,并且也得到了将整个int列表更改为str和将每个str更改为int之间的区别。你的规则[:你的代码非常棒。我真希望我能接受两个答案。你明确地教会了一个新东西去测试。