给定以下无序制表符分隔文件:
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) 在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) 我遇到了以下示例.我试着谷歌但找不到太多,所以我在这里发布这个问题.
这是代码:
#!/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) 我想用下面的代码打印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中做到这一点?
我遇到了以下片段(并且可以追溯到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=[]什么?
我执行下面提到的代码时遇到以下错误:
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)