python - 生成位置列表 (1, (2, 3), 4, (5, 6)) 如何?
问题描述
您好,我想知道如何为字符串切片的位置列表创建整数/元组列表。与标题相同的示例只是列出这种格式: (1, (2, 3), 4, (5, 6))
# my current attempt:
str_input, decrypted_str = "tester", ""
num_lists = [[x] + [(x + 1, x + 2)] for x in range(0, len(str_input), 4)]
for clist in num_lists:
for position in clist:
if isinstance(position, int):
decrypted_str += str_input[position]
else:
decrypted_str += str_input[position[0]:position[1]+1]
print(decrypted_str)
这导致“测试器”,但输出应该是测试器。
解决方案
最后一行之前的行应该是
decrypted_str += str_input[position[0]:position[1]+1]
因为 1:5 表示 1,2,3,4 而不是 5
推荐阅读
- r - 使用其他变量添加新列
- ios - 单击图像时如何在屏幕上随机移动 UIimageView
- cmake - 将现有的静态或共享库重新打包到 CMake 中的“find_package”目标中,而无需访问库的源
- php - Twilio 的 PHP 代码在终端中工作,但在浏览器中不工作
- r - 根据自定义用户输入 ggplot2 按顺序排列图
- chat - hangouts-chat:hangouts 聊天机器人无法将消息发布到 Bot 实现 https 端点
- swift - Firebase 身份验证 - UpdateEmail - 不要发送确认电子邮件
- java - 如何使用用于 graphstream 的 java API 删除节点?
- ios - iOS:解码 YUY2 视频帧
- php - WooCommerce AJAX 删除导航车的按钮