当前位置: 动力学知识库 > 问答 > 编程问答 >

python 3.x - How to find every item in list who's index is two to the x power (2, 4, 8, 16 etc.)?

问题描述:

I have a list that has 500 items and I want to get every item that is equal to 2 to the x power to be renamed False.

The best I could come up with is this

items = []

x = 2

for i in range(0,9):

for i in range(0,len(items)):

if i+1 == 2**x:

items[i] = False

x += 1

Please help, thank you!

网友答案:
items = [123,234,100,50, 64, 25,4,22,32,45,16, 9]

x = max(items)

def maxof2(x):
    for i in range(0,9): # You can choose the range based on the max value in the list
        if 2**i < x:
            continue
        else:
            return(i)

twopowers = [2**x for x in range(0,maxof2(x))]

for i in range(len(twopowers)):
    if twopowers[i] in items:
        items[i] = False

items

[123, 234, False, 50, False, False, 4, 22, 32, 45, 16, 9]

分享给朋友:
您可能感兴趣的文章:
随机阅读: