[英]How can I optimize the intersections between lists with two elements and generate a list of lists without duplicates in python?
[英]How can i retrieve elements of a list between two numbers in python?
我試圖輸出大於 39 但小於 291 的列表元素,所需的輸出為 49。
我的代碼如下,我想我剛剛犯了一個小錯誤,因為輸出幾乎是正確的,如果有人能幫助我,我會很感激,因為 python 對我來說是新手。
list=[1,2,30,49,29,299,291,39,30]
for x in list:
if x > 39 & x < 291:
print(x)
這是一種有效的方法,無需將自己與and
和&
混淆
lst=[1,2,30,49,29,299,291,39,30]
[print(x) for x in lst if 39 < x < 291]
注意:你不應該使用
list
作為變量名,因為它是轉換操作的名稱; 例如list()
您正在使用&
而不是and
,這將不起作用(Python 會拋出錯誤)。
list=[1,2,30,49,29,299,291,39,30]
for x in list:
if x > 39 and x < 291:
print(x)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.