小编daw*_*awg的帖子

火车路线的字典最佳数据结构?

所以我的任务基本上是读取一个文件(记事本文件),该文件有一堆列车停靠点以及从一站到另一站的时间.例如,它看起来像:

Stop A     15
Stop B     12
Stop C     9
Run Code Online (Sandbox Code Playgroud)

现在我需要返回并访问这些站点及其时间.我正在考虑阅读文件并将其存储为字典.我的问题是,字典是否最适合这个?或者是否有一些其他python工具会更有用?任何想法将不胜感激!

python routing dictionary

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

为什么这个.pop(0)像这样工作

def f(x, y):
   x.append(x.pop(0))
   x.append(y[0])
   return x

a = [4,5]
b = [1,2,3]
w = f(a,b) + f(a,b)
Run Code Online (Sandbox Code Playgroud)

w原来是[4,1,5,1,4,1,5,1]然而如果我手动完成代码(在我脑海中),我得到[5,4,1,4,1,5] ,1]

为什么是这样?

f(a,b)= [5,4,1]对吗?那么a将是= [5,4,1]和b = [1,2,3].

那么f(a,b)= [4,1,5,1]就是:[5,4,1] + [4,1,5,1] = [5,4,1,4,1 ,5,1]不[4,1,5,1,4,1,5,1]

为什么代码会改变这个?在尝试自己运行代码时,我做错了什么?

python python-2.7

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

Bash =〜在cmd提示符OS X处丢失BASH_REMATCH内容

假设我有一个(愚蠢的例子)脚本:

#!/bin/bash
st="one two three"
[[ $st =~ ^([[:alpha:]]+)[[:space:]]([[:alpha:]]+)[[:space:]]([[:alpha:]]+) ]]
for i in "${BASH_REMATCH[@]}"
do
    echo "$i"
done    
Run Code Online (Sandbox Code Playgroud)

它按预期工作 - 它打印:

one two three
one
two
three
Run Code Online (Sandbox Code Playgroud)

这是每个匹配组的总体匹配.但是,如果我去shell并输入:

$ st="one two three"
$ [[ $st =~ ^([[:alpha:]]+)[[:space:]]([[:alpha:]]+)[[:space:]]([[:alpha:]]+) ]]
$ for i in "${BASH_REMATCH[@]}"
> do
>    echo "$i"
> done
Run Code Online (Sandbox Code Playgroud)

它打印:

w
Run Code Online (Sandbox Code Playgroud)

如果我做

$ [[ $st =~ ^([[:alpha:]]+)[[:space:]]([[:alpha:]]+)[[:space:]]([[:alpha:]]+) ]] && arr=( "${BASH_REMATCH[@]}" )
Run Code Online (Sandbox Code Playgroud)

我然后可以通过副本循环BASH_REMATCHarr显示出比赛的作品确定.但是BASH_REMATCH在Bash 3.2上的交互式shell使用中似乎发生了一些事情.在成功比赛之后.

这是3.2.57(1)-releasemacOS Sierra 10.12.1 的默认设置

如果我启动Bash 4.4,一切正常.

想法? …

macos bash

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

如何将 Bash 的 glob 数组与 find 一起使用?

我有一组想要提供给的 glob find

我的数组是:

arr=('a*.txt' 'b[2-5].sh' 'ab?.doc')
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的:

find . -type f -name '${arr[@]}'
Run Code Online (Sandbox Code Playgroud)

这里我们的数组可能包含很多元素!感谢您的答复!

bash

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

Perl:cmpthese文本vs传递参数的匿名子问题

如果您cmpthesePerl Benchmark模块的文档中阅读过,它会说明cmpthesetimethese可以在文本或子例程引用中使用代码.文档似乎暗示这些表单是完全可以互换的:

# Use Perl code in strings...
timethese($count, {
'Name1' => '...code1...',
'Name2' => '...code2...',
});
# ... or use subroutine references.
timethese($count, {
'Name1' => sub { ...code1... },
'Name2' => sub { ...code2... },
});
Run Code Online (Sandbox Code Playgroud)

我遇到了传递带有字符串形式与子程序引用形式的参数的困难cmpthese.要么@array没有传递值,要么我有运行时错误.

我有以下代码:

#!/usr/bin/perl
use strict; use warnings;
use Benchmark qw(:all);

my @array = qw( first second third );

sub target {
    my $str =  $_[0];
    print "str=$str\n";
}

sub control …
Run Code Online (Sandbox Code Playgroud)

parameters perl eval subroutine

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

如何遍历python的字典列表

这是我的字典:

[{'entity': 'first entity', 'place': ['first', 'second', 'abc']}, {'entity': 'second entity', 'place': ['awe', 'ert']}]
Run Code Online (Sandbox Code Playgroud)

我想以这种方式打印值:

-first entity
-first, second, abc

-second entity
-awe, ert
Run Code Online (Sandbox Code Playgroud)

我尝试了很多东西,但我不知道如何处理第二个键的列表

你能否建议我如何在Django模板中做同样的事情?

提前致谢

python django dictionary

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

将列表转换为元组的分组列表?

我有这个清单:

['Ben\n', '5 0 0 5 -5 \n', 'Moose\n', '5 5 0 3 0 0 \n', ...]
Run Code Online (Sandbox Code Playgroud)

这种情况持续了很长时间.我需要将此列表放入这样的元组列表中:

[('Ben', '5 0 0 5 -5'), ('Moose', '5 5 0 3 0 0'), ...] and so on.
Run Code Online (Sandbox Code Playgroud)

我觉得这应该很容易.
我已经打开了文本文件,然后通过这种方式将其拆分为一个列表:

e = open("bookratings.txt", "r")
elines = e.readlines()
Run Code Online (Sandbox Code Playgroud)

但我不知道从哪里开始?

python

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

PyMongo - 将属性中的所有值设置为小写

我正在清理一个数据集,并且有一个字段性别。在该字段中,有“男性”、“男性”和“男性”等条目。为了解决这个问题,我正在尝试使用 pymongo 更新我的 MongoDB 数据库。

在数据库中,Gender属性是Gender(前面有一个大写的G)

我的代码目前看起来像这样:

import pymongo
from pymongo import MongoClient


db_info = {
    'db_name': 'MentalHealth',
    'collection_name': 'MentalHealth',
}

if __name__ == "__main__":

    mongo_client = MongoClient()
    mongo_db = mongo_client[db_info['db_name']]
    mongo_collection = mongo_db[db_info['collection_name']]

    #normalize to lowercase
    mongo_collection.aggregate([{ '$project': { 'Gender':{ '$toLower':"$Gender"}}}])
Run Code Online (Sandbox Code Playgroud)

代码运行没有问题,但数据库没有更新,我不确定代码有什么错误。任何帮助将不胜感激。谢谢!!!

python mongodb pymongo

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

将带有键和值的字符串转换为python dict的最有效方法

如何转换字符串

{"key"=>"Number of Contents in Sales Package", "value"=>"Pack of 3"}
Run Code Online (Sandbox Code Playgroud)

进入像这样的字典

{'Number of Contents in Sales Package':'Pack of 3'}?
Run Code Online (Sandbox Code Playgroud)

python regex string dictionary

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

带有函数的 Ruby 迭代器。返回函数的第一个值而不迭代整个列表

我有一个数组:arr=[x1, x2, x3...]和一个函数,该函数基于第一个函数返回一个值x,因为arr该函数是真实的。

本质上:

# my_x is the return of func() 
# with the first x in arr that func(x) is true
# and the entire arr array is not processed.

my_x=arr.ruby_magic{|x| func(x) } 

my_x should be equal to first true value return of func(x)
Run Code Online (Sandbox Code Playgroud)

假设每个Xinarr都是一个正则表达式模式。无需运行每个正则表达式,我想返回第一场比赛的捕获组。

在 Python 中,我会用next. 它将运行每个谓词,直到返回真值,然后将该值传递给m。如果没有 true return,None则用作默认值,但该默认值可以是任何值:

import re 

patterns=[r"no match", r": (Value.*?pref)", r": (Value.*)", r"etc..."]

s=""" 
This is …
Run Code Online (Sandbox Code Playgroud)

ruby

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