首页 > 解决方案 > 我们如何打印列表值的自定义值

问题描述

我必须从值中获取输入,如下所示

def abc():
    d = [1, 2, 3]

    if d:       
     print('select')  
     i = 0
    while i < len(d):
        output = d[i].join(d) +'is value'
        print(output)
        i=i+1
        if i < len(d):
         print('or')
        # print ('select',+str(output))
abc()

我期望的是打印输出如下,并使用 xlswriter 在 excel 中编写相同的内容:

选择 1 是值或 2 是值或 3 是值

但我收到如下所示的错误:

TypeError: sequence item 0: expected str instance, int found

标签: python

解决方案


d[i]是一个整数,整数没有连接函数,你可以直接将它附加到字符串中

f'{d[i]} is value'

如果您确实想使用连接,并一次完成整个操作..

print('select ', ' or '.join([f'{val} is value' for val in d]))

推荐阅读