相关疑难解决方法(0)

python迭代列表

我尝试迭代列表中的每个项目.但迭代器不会遍历所有对象.这是我的功能代码(dirs和root来自os.walk)

def remove_hidden_dirs(dirs,root):
    """
    Function remove hidden directories . .. in unix
    """
    logging.debug("\n\n remove hidden" )
    logging.debug('Start removing hidden dirs for %s',root)
    logging.debug("Length of dirs %s",len(dirs))
    logging.debug('File list before removing')
    dirs.sort()
    for d in dirs:
        logging.debug(d)
    i=0
    for d in dirs:
        i=i+1
        logging.debug("Iterating over %s",d)
        if d[0:1] == '.' or d[0:2] =='..':
            dirs.remove(d)
            logging.debug("Dir %s is being removed because of being hidden dir",d)
        else:
            logging.debug("Dir %s is NOT being removed because of being hidden dir",d)
    logging.debug("Iterate on %s", i) …
Run Code Online (Sandbox Code Playgroud)

python iteration list

1
推荐指数
1
解决办法
9635
查看次数

isinstance()方法python返回错误的值

list_d = ["a","b","c",3,5,4,"d","e","f",1,"ee"]
list_e = []

print("Before: %s"%(list_d))
print("Before: %s"%(list_e))

for item in list_d:
    if isinstance(item,int):
        list_d.pop(item)
        list_e.append(item)

print("After: %s"%(list_d))
print("After: %s"%(list_e))
Run Code Online (Sandbox Code Playgroud)

我有这个代码.我想将list_d中的这些数字传递给list_e,但结果是:

Before: ['a', 'b', 'c', 3, 5, 4, 'd', 'e', 'f', 1, 'ee']
Before: []
After: ['a', 'c', 5, 'd', 'e', 'f', 1, 'ee']
After: [3, 4, 1]
Run Code Online (Sandbox Code Playgroud)

不知何故5和1没有弹出,1附加到list_e但5不是.我的代码出了什么问题?

python isinstance

0
推荐指数
1
解决办法
57
查看次数

Python startswith() 不删除所有匹配项

不确定我是否在这里做任何愚蠢的事情,但我试图删除列表中的所有项目,该列表以00:00:00.但并非所有项目都匹配。

tc = ['00:00:00.360', '00:00:00.920', '00:00:00.060', '00:00:02.600', '00:00:05.960', '00:00:01.040', '00:00:01.140', '00:00:01.060', '00:00:01.480', '00:00:00.140', '00:00:00.280', '00:00:01.200', '00:00:00.400', '00:00:01.220', '00:00:00.380']
for item in tc:
    if item.startswith(str('00:00:00.')):
        tc.remove(item)
print (tc)
Run Code Online (Sandbox Code Playgroud)

结果:

['00:00:00.920', '00:00:02.600', '00:00:05.960', '00:00:01.040', '00:00:01.140', '00:00:01.060', '00:00:01.480', '00:00:00.280', '00:00:01.200', '00:00:01.220']

预期结果:

['00:00:02.600', '00:00:05.960', '00:00:01.040', '00:00:01.140', '00:00:01.060', '00:00:01.480', '00:00:01.200', '00:00:01.220']

知道这里可能是什么问题吗?

python

0
推荐指数
1
解决办法
52
查看次数

for循环如何在python中实际工作

我曾经认为python中的for 循环是这样工作的,它首先通过执行iter(iterable) then 来创建一个迭代器,然后next(that_new_iterator_object) 当它引发时,StopIteration然后for 循环结束并else阻塞(如果提供),但在这里它的工作方式有所不同

>>> a = [1,2,3,4,5,6,7,8,9]
>>> for i in a:
        del a[-1]
        print(i)

1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)

其他数字 6,7,8,9 在哪里 for 循环创建的新迭代器对象和变量 a 不同

python for-loop python-3.x

0
推荐指数
1
解决办法
75
查看次数

如何从列表中删除一些元素并将它们附加到 Python 中的列表开头

假设我有一个列表,其中包含 [0, 1, 2, 3 , 4, 5, 6] 。我想删除那些大于或等于 3 的元素并将这些删除的元素添加到列表的开头。所以我写了下面的代码:

list = [0, 1, 2, 3, 4, 5, 6]
new_list =[]

for number in list:
    if number >= 3:
        dropped_number = list.pop()
        new_list.append(dropped_number)
    new_list.sort()

new_list += list

print(new_list)
Run Code Online (Sandbox Code Playgroud)

但是,当我运行代码时,结果显示为 [5, 6, 0, 1, 2, 3 , 4]。谁能向我解释一下我在这里做错了哪一步?

python

-2
推荐指数
1
解决办法
58
查看次数

标签 统计

python ×5

for-loop ×1

isinstance ×1

iteration ×1

list ×1

python-3.x ×1