小编Sac*_*n S的帖子

Python - 创建层次结构文件(在表示为表的树中查找从根到叶子的路径)

给定以下无序制表符分隔文件:

Asia    Srilanka
Srilanka    Colombo
Continents  Europe
India   Mumbai
India   Pune
Continents  Asia
Earth   Continents
Asia    India
Run Code Online (Sandbox Code Playgroud)

目标是生成以下输出(制表符分隔):

Earth   Continents  Asia    India   Mumbai
Earth   Continents  Asia    India   Pune
Earth   Continents  Asia    Srilanka    Colombo
Earth   Continents  Europe
Run Code Online (Sandbox Code Playgroud)

我创建了以下脚本来实现目标:

root={} # this hash will finally contain the ROOT member from which all the nodes emanate
link={} # this is to hold the grouping of immediate children 
for line in f:
    line=line.rstrip('\r\n')
    line=line.strip()
    cols=list(line.split('\t'))
    parent=cols[0]
    child=cols[1]
    if not parent in link:
        root[parent]=1
    if …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

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

批处理 - 如何从批处理脚本本身重定向stderr和stdout?

在Unix shell脚本中,可以从内部脚本本身重定向stderr和stdout,如下所示:

#!/bin/ksh
# script_name: test.sh
export AUTO_LOGFILE=`basename $0 .sh`.log
# stdout and stderr Redirection. This will save the old stdout on FD 3, and the old stderr on FD 4.
exec 3>&0 4>&1 >>$AUTO_LOGFILE 2>&1
echo "Hello World"
# The above echo will be printed to test.log
Run Code Online (Sandbox Code Playgroud)

实际上,test.sh可以简单地执行:

test.sh
Run Code Online (Sandbox Code Playgroud)

代替:

test.sh >> test.log 2>&1    
Run Code Online (Sandbox Code Playgroud)

我试图在批处理脚本中做类似的事情.我的批处理代码如下:

@echo off & setlocal enableextensions enabledelayedexpansion
REM script_name=test.bat
set AUTO_LOGFILE=%~n0.log
REM How to do the stdout and stderr redirection from within the script itself …
Run Code Online (Sandbox Code Playgroud)

batch-file

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

使用eval在shell脚本中执行perl代码

我遇到了以下示例.我试着谷歌但找不到太多,所以我在这里发布这个问题.

  1. 执行像这样的perl脚本有什么好处?
  2. 一旦我们执行perl代码,我们如何使shell脚本像"普通"shell脚本一样工作?

这是代码:

#!/bin/ksh
#! -*- perl -*-
eval 'exec $PERLLOCATION/bin/perl -x $0 ${1+"$@"} ;'
if 0;

print "hello world\n";
# how can I make it behave like a "normal" shell script from this point onwards? What needs to be done?
# echo "hello world" ### this results in error
Run Code Online (Sandbox Code Playgroud)

unix perl

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

Python如何打印列表列表

我想用下面的代码打印python 3.x中的列表列表,但是它给出了一个错误.

lol=[[1,2],[3,4],[5,6],['five','six']]
for elem in lol:
      print (":".join(elem))
# this is the error I am getting-> TypeError: sequence item 0: expected str instance, int found
Run Code Online (Sandbox Code Playgroud)

我期待这个输出:

1:2
3:4
5:6
five:six
Run Code Online (Sandbox Code Playgroud)

我可以使用下面的perl代码实现相同的输出(这仅供参考):

for (my $i=0;$i<scalar(@{$lol});$i++)
{
    print join(":",@{$lol->[$i]})."\n";
}
Run Code Online (Sandbox Code Playgroud)

我怎么在python 3.x中做到这一点?

python python-3.x

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

Python列表初始化:[] vs. [[]]

我遇到了以下片段(并且可以追溯到https://docs.python.org/3/library/itertools.html#itertools.product):

def cartesian_product(pools):
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    return result

a_list=[1, 2, 3]
b_list=[4, 5]
all_list=[a_list, b_list]

print (cartesian_product(all_list)) # [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]
Run Code Online (Sandbox Code Playgroud)

如果我们更改以下行:

result = [[]]
Run Code Online (Sandbox Code Playgroud)

对此:

result = []
Run Code Online (Sandbox Code Playgroud)

然后代码不起作用.

现在考虑下面的一段代码,其中变量my_list初始化为my_list=[]和不是,my_list=[[]]但我们仍然得到预期的结果:

my_list=[]
my_list.append([1,2])
my_list.append([3,4])
print (my_list) # [[1, 2], [3, 4]]    
Run Code Online (Sandbox Code Playgroud)

所以在cartesian_product我上面提到的函数中,有result=[[]]和没有意义是result=[]什么?

python python-3.x

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

调用泛型方法

我执行下面提到的代码时遇到以下错误:

TestGenerics1.java:40: error: cannot find symbol
                              arr.get(i).eat();
                                        ^
symbol:   method eat()
location: class Object
1 error
Run Code Online (Sandbox Code Playgroud)

我将面临的问题在以下示例代码的帮助下重现:

import java.util.*;

abstract class Animal
{
    void eat() { System.out.println("animal eating"); }
}

class Dog extends Animal
{
    void bark() { }
}

class Cat extends Animal
{
    void meow() { }
}

class RedCat extends Cat { }

public class TestGenerics1
{
    public static void main(String[] args)
    {
        new TestGenerics1().go();
    }

    public void go()
    {
        List<Cat> arrAnimals = new ArrayList<Cat>(Arrays.asList(new RedCat(), new …
Run Code Online (Sandbox Code Playgroud)

java

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

标签 统计

python ×3

python-3.x ×3

batch-file ×1

java ×1

perl ×1

unix ×1