網站首頁 編程語言 正文
如何查找列表中元素的位置
問題描述
給出一個包含n個整數的數列,問整數a在數列中的第一次出現是第幾個。
輸入格式
第一行包含一個整數n。
第二行包含n個非負整數,為給定的數列,數列中的每個數都不大于10000。
第三行包含一個整數a,為待查找的數。
- 樣例輸入
6
1 9 4 8 3 9
9
- 樣例輸出
2
n = int(input())
x = list(map(int,input().split()))
a = int(input())
if a in x:
b = x.index(a)
print(b+1)
else:
print(-1)
python中list()查找方法
list五種查找方式
in,not in,count,index,find前兩種方法是保留字,后兩種是列表的方法
函數EXA
#判斷值a是否在列表中,并返回True或False
'a' in lst
'a' not in lst
#統計指定值在列表中出現的次數,count(value)方法:
lst.count('a')
#index返回指定值在列表中的位置,不存在返回異常,index(value,,[start,stop])
lst.index('a')
#find函數,string類型才能用,查看指定值在列表中的位置
#如果找到則返回第一個匹配的位置,如果沒找到則返回-1,而如果通過index方法去查找的話,沒找到的話會報錯。
string="xiaojiejie"
string.find('x')
原文鏈接:https://blog.csdn.net/weixin_56817152/article/details/123340605
相關推薦
- 2023-07-08 CMake Error at CMakeLists.txt:4 (find_package): By
- 2022-11-29 React?中?memo?useMemo?useCallback?到底該怎么用_React
- 2022-05-03 ASP.NET?Core基于滑動窗口實現限流控制_實用技巧
- 2024-01-30 關閉scrapy的UserWarning: Selector got both text and r
- 2022-06-17 C#使用讀寫鎖解決多線程并發問題_C#教程
- 2022-05-02 深入了解Python?中線程和進程區別_python
- 2022-12-14 Rocksdb?Memtable數據結構源碼解析_Android
- 2022-08-12 C++鏈表實現通訊錄設計_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支