python - 如何在某些索引处显示包含某些元素的列表?
问题描述
如果所述列表在某些位置包含某些元素,我一直在尝试让 Python 2.7 显示列表(或字符串、元组等)中的任何内容。
假设我知道对于列表l
,l[1]
应该是3
,l[2]
应该是1
,l[5]
应该是5
。如果这些条件成立,我想打印l
,但我坚持编写contains_at
函数。
indexes = (1, 2, 5)
elements = (7, 1, 8)
l1 = [2, 7, 1, 8, 2, 8, 1]
l2 = [3, 1, 4, 1, 5, 9, 2, 6, 5]
def print_if_contains_at(l, idxs, elts):
# some function that returns True if I should print the list
if contains_at(l, idxs, elts):
print(l)
# prints the entire list l1
print_if_contains_at(l1, indexes, elements)
# prints nothing, since l2 doesn't contain the correct pattern of elements
print_if_contains_at(l2, indexes, elements)
解决方案
我能想出的最接近的解决方案是做同样工作的最刚性和最不灵活的解决方案。这适用于少量列表,但是编写无数步骤会很详尽。
a = input('Enter the string')
b,c,d = input('Enter the the three index elements you are looking for')
if a in b[0:3]:
print(string)
推荐阅读
- flutter - Flutter 错误:在 null 上调用了方法“验证”。接收方:null 尝试调用:validate()
- flutter - 底部溢出 55 个像素的 RenderFlex (Android)
- python - Pandas 导致 EXE 文件(使用 PyInstaller)具有巨大的大小(几乎 0.5 GB)
- r - R:使用 .csv 文件的名称作为一行创建数据框,文件中的错误(文件,“rt”):无效的“描述”参数
- php - Twitter 流 api 打印状态
- julia - 导入 julia 包时运行 Pkg.instantiate() 错误
- installation - 如何将 Windows Installer 创建的两个 msi 安装包(64 位和 32 位)合二为一?
- python - 在第二次出现子字符串后打印字符串的其余部分
- sql-server - Grails 无法使用 SQL Server 数据库将序列下一个值分配给字段
- javascript - ActionChain onclick().perform() 后 Python selenium 进程退出