python - 我们如何打印列表值的自定义值
问题描述
我必须从值中获取输入,如下所示
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
解决方案
d[i]
是一个整数,整数没有连接函数,你可以直接将它附加到字符串中
f'{d[i]} is value'
如果您确实想使用连接,并一次完成整个操作..
print('select ', ' or '.join([f'{val} is value' for val in d]))
推荐阅读
- php - 如何更新 Symfony 实体类中的变量?
- spring-security - Spring Security 自定义登录回退
- r - 如何使用 RColorBrewer 调色板设置 ggplot 全局颜色主题
- typescript - 从打字稿导入 SCSS
- c - 使用 `CLONE_NEWUSER|CLONE_NEWNS` 调用克隆时,挂载传播的行为如何?
- javascript - Javascript 对象到 json 或 pytrhon dic 以获取值
- docker - Docker默认桥接网络在wsl2中不起作用
- python - 我的代码不断出现错误,并且它无法正常工作我不知道我是否做错了什么?
- sql - 在 SQL 查询中以“2019-05-04 11:20:22.697”的格式检索特定年份
- python - NameError:名称“变量名”未定义笔记本