Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:如何在列表中返回2列/索引_Python - Fatal编程技术网

python:如何在列表中返回2列/索引

python:如何在列表中返回2列/索引,python,Python,我有一个使用for循环生成的列表。它返回: home1-0002_UUID 3457077784 2132011944 1307504896 62% home1-0003_UUID 3457077784 2088064860 1351451980 61% home1-0001_UUID 3457077784 2092270236 1347246604 61% 如何仅返回第三列和第五列 编辑 当我得到一个错误时,它会说“Nonetype”对象是不可编辑的 f

我有一个使用for循环生成的列表。它返回:

home1-0002_UUID    3457077784  2132011944  1307504896  62%
home1-0003_UUID    3457077784  2088064860  1351451980  61%
home1-0001_UUID    3457077784  2092270236  1347246604  61%
如何仅返回第三列和第五列

编辑 当我得到一个错误时,它会说“Nonetype”对象是不可编辑的

 for index, elem in enumerate(my_list):
    print (index,elem)
我还尝试使用list(enumerate(my_list))获取索引,但不起作用。我得到了TypeError:“NoneType”对象不可iterable

 for index, elem in enumerate(my_list):
    print (index,elem)
以下是我填充列表的方式:

def h1ost():
    p1 = subprocess.Popen("/opt/lustre-gem_s/default/bin/lfs df /lustre/home1 | sort -r -nk5",stdout=subprocess.PIPE, shell=True)
    use = p1.communicate()[0]
    for o in use.split('\n')[:6]:
        if "Available" not in o and "summary" not in o:
            print (o)

至于我不能发表评论,我会尽我所能给你一个问题的解决方案

def empty_string_filter(value):
    return value != ''

def h1ost():
    p1 = subprocess.Popen("/opt/lustre-gem_s/default/bin/lfs df /lustre/home1 | sort -r -nk5",stdout=subprocess.PIPE, shell=True)
    use = p1.communicate()[0]
    new_file_content_list = [] 
    # Separate by line break and filter any empty string
    filtered_use_list = filter(empty_string_filter, use.split(os.linesep)[:6])
    for line in filtered_use_list :
        # Split the line and filter the empty strings in order to keep only
        # columns with information
        split_line = filter(empty_string_filter, line.split(' '))
        # Beware! This will only work if each line has 5 or more data columns
        # I guess the correct option is to check if it has at least 5 columns
        # and if it hasn't do not store the information or raise an exception. 
        # It's up to you.

        new_file_content_list.append('{0} {1}'.format(split_line[2] , split_line[4]))

    return os.linesep.join(new_file_content_list)

因此,我们的想法是用空格分隔每一行,并过滤剩余的空字符串,以获得第3列和第5列(分别为索引2和索引4)

列表是如何分隔的?你能把实际产量加起来吗?这是一个列表还是一个数据帧?
[[col[2],col[4]]表示列表中的col]
?这是一个数组数组吗?我用错误更新了问题。我使用一个子进程来使用find命令,该命令从文件系统返回列表。我希望这是有道理的。请把你的问题表现出来。i、 e.显示不起作用的代码。感谢您的回复。这很有效。结果是h1ost()是否可以将其格式化回原始状态,或者通过将其写入文件来实现?我修改了答案,以便检索准备写入文件的单个字符串。通常,连接将在外部函数中完成,以便进行数据管理。这非常有效!当我打印报税表时,报税表中的\n行完全对齐。