从数组中删除列表符号

时间:2018-01-21 13:05:02

标签: python list

我将元素存储在数组中,但它们存储在列表中。如何在没有列表的情况下获取它们?这就是我的意思

for folders in os.listdir(folder_path):
    for folder in os.listdir(folder_path+folders):
        if folder == 'images':
            image_file = os.listdir(folder_path+folders+'/'+'images'+'/')
            allFileNames.append(image_file)`

我得到的是

for i in allFileNames:
    print (i)


['a31deaf0ac279d5f34fb2eca80cc2abce6ef30bd64e7aca40efe4b2ba8e9ad3d.png']
['308084bdd358e0bd3dc7f2b409d6f34cc119bce30216f44667fc2be43ff31722.png']
['5953af5080d981b554529971903d8bee9871457a4361b51f04ba04f43793dd8f.png']

我理想的是

'a31deaf0ac279d5f34fb2eca80cc2abce6ef30bd64e7aca40efe4b2ba8e9ad3d.png'
'308084bdd358e0bd3dc7f2b409d6f34cc119bce30216f44667fc2be43ff31722.png'
'5953af5080d981b554529971903d8bee``9871457a4361b51f04ba04f43793dd8f.png'
'29dd28df98ee51b4ab1a87f5509538ecc3e4697fc57c40c6165658f61b0d8e3a.png'

关于我如何做到这一点的任何建议?

2 个答案:

答案 0 :(得分:3)

我认为你可以替换

allFileNames.append(image_file)

通过

allFileNames.extend(image_file)

查看this thread了解详情。

答案 1 :(得分:1)

您可以尝试使用Generators:

  allFileNames = (os.listdir(folder_path+folders+'/'+'images'+'/') 
                    for folders in os.listdir(folder_path) 
                    for folder in os.listdir(folder_path+folders) if folder == "images")
  print(*list(allFileNames),sep="\n") #In python 2, need to import this from __future__ import print_function