小编Ale*_*dro的帖子

bash脚本:如果参数等于此字符串,则定义一个类似于此字符串的变量

我正在做一些bash脚本,现在我有一个变量调用source和一个数组调用samples,如下所示:

source='country'
samples=(US Canada Mexico...)
Run Code Online (Sandbox Code Playgroud)

因为我想扩展源的数量(并且每个源都有自己的样本)我试图添加一些参数来做到这一点.我试过这个:

source=""
samples=("")
if [ $1="country" ]; then
   source="country"
   samples="US Canada Mexico..."
else
   echo "try again"
fi
Run Code Online (Sandbox Code Playgroud)

但是当我运行我的脚本时source countries.sh country它没有用.我究竟做错了什么?

bash scripting arguments

205
推荐指数
3
解决办法
37万
查看次数

python字典中的5个最大值

我有一个这样的字典:

A = {'a':10, 'b':843, 'c': 39,.....}
Run Code Online (Sandbox Code Playgroud)

我想得到这个dict的5个最大值,并用这个存储一个新的dict.为了获得我做的最大值:

max(A.iteritems(), key=operator.itemgetter(1))[0:]
Run Code Online (Sandbox Code Playgroud)

也许这是一件容易的事,但我长期坚持下去.请帮忙!!!

python dictionary max

52
推荐指数
4
解决办法
7万
查看次数

排序值并从dict python返回键列表

可能重复:
Python:按值对字典排序

我有一个这样的字典:

A = {'Name1':34, 'Name2': 12, 'Name6': 46,....}
Run Code Online (Sandbox Code Playgroud)

我想要一个按值排序的键列表,即 [Name2, Name1, Name6....]

谢谢!!!

python dictionary

36
推荐指数
3
解决办法
3万
查看次数

使用函数不适用于getopt

我在Python中使用函数有问题.这是我主要功能的一部分:

def main(argv):
    try:
            opts, args = getopt.getopt(argv, 'hi:o:tbpms:', ['help', 'input=', 'output='])
            if not opts:
                    print 'No options supplied'
                    usage()
    except getopt.GetoptError,e:
           print e
           usage()
           sys.exit(2)

    for opt, arg in opts:
            if opt in ('-h', '--help'):
                    usage()
                   sys.exit(2)
if __name__ =='__main__':
    main(sys.argv[1:])
Run Code Online (Sandbox Code Playgroud)

我也定义了一个使用函数

def usage():
    print "\nThis is the usage function\n"
    print 'Usage: '+sys.argv[0]+' -i <file1> [option]'
Run Code Online (Sandbox Code Playgroud)

但当我运行我的代码./code.py./code.py -h(它是可执行的)时,我得到了除Python帮助之外的任何东西.

python getopt command-line-arguments

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

在嵌套的Python字典中搜索密钥

我有一些像这样的Python词典:

A = {id: {idnumber: condition},.... 
Run Code Online (Sandbox Code Playgroud)

例如

A = {1: {11 : 567.54}, 2: {14 : 123.13}, .....
Run Code Online (Sandbox Code Playgroud)

我需要搜索字典中是否有任何字典idnumber == 11并使用condition.但如果在整个字典中没有idnumber == 11,我需要继续下一个字典.

这是我的尝试:

for id, idnumber in A.iteritems():
    if 11 in idnumber.keys(): 
       calculate = ......
    else:
       break
Run Code Online (Sandbox Code Playgroud)

python recursion dictionary nested

5
推荐指数
2
解决办法
8655
查看次数

获取字典中最大键的值(python)

我只在这里找到了如何获得最大值的密钥:

max(d, key=d.get())
Run Code Online (Sandbox Code Playgroud)

但我需要搜索最大键并返回此键的值.

谢谢,

python dictionary

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

无法使用getopt python处理参数

为了给我的python脚本提供选项,我想介绍一些参数.我发现在python中更好的方法是使用getopt,但是一旦我运行我的脚本它就什么都不做.请帮我!!!.这是我的代码:

def main(argv):
     try:
            opts, args = getopt.getopt(argv, 'hi:o:t', ['help', 'input=', 'output='])
    except getopt.GetoptError:
            usage()
            sys.exit(2)
            file = None
            outfile = None
    for opt, arg in opts:
            if opt in ('-h', '--help'):
                    usage()
                    sys.exit(2)
            elif opt in ('-i', '--input'):
                    file = arg
            elif opt in ('-o', '--output'):
                    outfile = arg
            elif opt == '-t':
                    maininfo(file,outfile)
            else:
                    usage()
                    sys.exit(2)

if __name__ =='__main__':
    main(sys.argv[1:])
Run Code Online (Sandbox Code Playgroud)

python getopt

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

字典python中的第二个最大值

我有一个常规词典,如:

A = {37:4783, 92:47834, 12:234234,....}
Run Code Online (Sandbox Code Playgroud)

我需要返回第二个最大值,第三个,依此类推.我正在尝试:

max(A, key=lambda x: x[1])
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误:TypeError:'float'对象是unsubscriptable

我究竟做错了什么?

谢谢

python dictionary

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