【问题标题】:How to modify array values?如何修改数组值?
【发布时间】:2017-02-04 18:42:53
【问题描述】:

我在将我的 Linux 技能转换为 python 时遇到了一些问题。任何指向正确方向的指针都将不胜感激。

概述

我有 2 个动态创建的列表;列表中的项目数量可能会根据许多不同的因素而变化。 为了这个例子,我创建了 2 个静态列表来表示从其他地方(file_root 和 docs_directory)提取的数据:

def find_directory_function():
        global full_path
        file_root = ['/home/work'] #number of values here can change!
        # number of values below can change!
        docs_directory = ['important.docs/','random.directory/', dev.stuff/]
        PATH = []
        full_path=[]

        for i in docs_directory:
                PATH = file_root.join(i)
                full_path.append(PATH)
                print full_path

find_directory_function()

期望

我希望输出是具有以下值的另一个数组(full_path):

full_path = ["/home/work/important.docs/",
             "/home/work/random.directory/",
             "/home/work/dev.stuff/",]

问题

  1. 正在引发以下异常:

    Traceback (most recent call last):
      File "test.py", line 15, in <module>
        find_directory_function()
      File "test.py", line 11, in find_directory_function
        PATH = file_root.join(i)
    AttributeError: 'list' object has no attribute 'join'
    
  2. 即使我设法操纵字符串并将它们放入数组中,它们仍然会在连接值的中间缺少一个“/”(斜杠)。

【问题讨论】:

  • 如果file_root是一个列表,如何将文件与目录连接起来?有一个组合?产品?
  • 试试os.path.join(root, i),file_root也应该是一个字符串,而不是字符串列表
  • 我从您的评论中假设 file_root 目录的数量可以更改。那是对的吗?如果是这样,请参阅我的答案。

标签: python arrays python-2.7


【解决方案1】:

使用os.path.join:

import os
import copy
def find_directory_function():
    global full_path
    file_root = ['/home/work'] #number of values here can change!
    docs_directory = ['important.docs/','random.directory/', dev.stuff/] #number of values here can change!
    PATH = []
    full_path=[]

    for i in docs_directory:
        # since file_root can be an array, use copy to grab a copy 
        # of the array
        args = copy.copy(file_root)
        # and stick `i` on the end of that array so
        # that we have our full param list for os.path.join
        args.append(i)
        PATH = os.path.join(*args)
        full_path.append(PATH)
        print full_path

find_directory_function()

【讨论】:

  • 这完美!谢谢你。我还不能投票,但这正是我需要的!
【解决方案2】:

file_root = ['/home/work'] 更改为file_root = '/home/work/'

或在for 循环中进行以下更改:

file_root[0] + "/" + i

【讨论】:

    【解决方案3】:

    怎么样

    import os.path
    # ...
    
    def make_full_paths(roots, paths):
        "roots is a list of prefixes. paths is a list of suffixes."
        full_paths = []
        for root in roots:
            for path in paths:
                full_paths.append(os.path.join(root, path))
        return full_paths
    

    还有其他方法你也可以做到这一点...作为带有yield 的生成器函数(可能不是你想要的,因为你可能想要多次使用full_paths);或使用 https://docs.python.org/2/library/itertools.html#itertools.product ,这将使其成为单线(更高级):

    import itertools
    import os.path
    # ...
    
    def make_full_paths2(roots, paths):
        "roots is a list of prefixes. paths is a list of suffixes."
        return [os.path.join(root, path) for root, path in itertools.product(roots, paths)]
    

    【讨论】:

      猜你喜欢
      • 2015-05-18
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多