小编new*_*bie的帖子

Python中有字典理解吗?(函数返回dict的问题)

我知道列表理解,字典理解怎么样?

预期产出:

>>> countChar('google')
    {'e': 1, 'g': 2, 'l': 1, 'o': 2}
    >>> countLetters('apple')
    {'a': 1, 'e': 1, 'l': 1, 'p': 2}
    >>> countLetters('')
    {}
Run Code Online (Sandbox Code Playgroud)

代码(我是初学者):

def countChar(word):
    l = []
    #get a list from word
    for c  in word: l.append(c)
    sortedList = sorted(l)
    uniqueSet = set(sortedList)
    return {item:word.count(item) for item in uniqueSet }
Run Code Online (Sandbox Code Playgroud)

这段代码有什么问题?为什么我这样做SyntaxError

return { item:word.count(item) for item in uniqueSet }
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

python dictionary python-2.x

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

在php中获得带有second.decimal-fraction-of-second日期的ISO 8601?

我回应这个:

  php> echo date("Y-m-d\TH:i:s");
      2011-05-27T11:21:23
Run Code Online (Sandbox Code Playgroud)

如何使用date函数获取此日期格式:

2011-01-12T14:41:35.7042252+01:00 (例如)

35.7042252 => seconds.decimal-fraction-of-second

我试过了:

php> function getTimestamp()
 ... {
 ...         return date("Y-m-d\TH:i:s") . substr((string)microtime(), 1, 8);
 ... }

php> echo getTimestamp();
2011-05-27T15:34:35.6688370 // missing +01:00 how can I do?
Run Code Online (Sandbox Code Playgroud)

php datetime

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

array_filter与assoc数组?

我使用array_filter做这样的事情:

function endswithy($value) {
    return (substr($value, -1) == 'y');
}

$people = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe");
$withy = array_filter($people, "endswithy");
var_dump($withy);
Run Code Online (Sandbox Code Playgroud)

但是在过滤器中有更多选项,例如

$people = array(
             "Johnny"=>array("year"=>1989, "job"=>"prof"),
             "Timmy"=>array("year"=>1989,  "job"=>"std"),
             "Bobby"=>array("year"=>1988),
             "Sam"=>array("year"=>1983),
             "Tammy"=>array("year"=>1985),
             "Danny"=>array("year"=>1983),
             "Joe"=>array("year"=>1989,"job"=>"prof"));
Run Code Online (Sandbox Code Playgroud)

要么

$people = array(
             array("name"=>"Johnny","year"=>1989, "job"=>"prof"),
             array("name"=>"Timmy","year"=>1989,  "job"=>"std"),
             array("name"=>"Bobby""year"=>1988),
             array("name"=>"Sam","year"=>1983),
             array("name"=>"Tammy","year"=>1985),
             array("name"="Danny","year"=>1983),
             array("name"="Joe","year"=>1989,"job"=>"prof"));
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到这些人(endwith yyear=1989 and job=prof),我可以使用array_filter吗?或任何内置函数来做到这一点?

$people = array(
                 "Johnny"=>array("year"=>1989, "job"=>"prof")
  );
Run Code Online (Sandbox Code Playgroud)

要么

$people = array(
                 array("name="Johnny","year"=>1989, "job"=>"prof")
  );
Run Code Online (Sandbox Code Playgroud)

php multidimensional-array

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

php在@attributes上读取xml文件循环?

我有一个exrate.xml看起来像这样

 <!--For reference only. Only one request every 5 minutes!-->
    <ExrateList>
    <DateTime>5/29/2011 8:54:12 PM</DateTime>
    <Exrate CurrencyCode="AUD" CurrencyName="AUST.DOLLAR" Buy="21688.77" Transfer="21819.69" Sell="22201.6"/>    
    <Source>source name </Source>
    </ExrateList>
Run Code Online (Sandbox Code Playgroud)

有人知道如何读取xml并输出数据.

货币| 买| 拍卖

我用过

            <?php
              = simplexml_load_file("Service/Forex_Content.xml");
            echo '<pre>';
            print_r($xml);
            echo '</pre>';

        ?>


SimpleXMLElement Object
(
    [DateTime] => 5/29/2011 8:54:12 PM
    [Exrate] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [CurrencyCode] => AUD
                            [CurrencyName] => AUST.DOLLAR
                            [Buy] => 21688.77
                            [Transfer] => 21819.69
                            [Sell] => 22201.6
                        )

                )
Run Code Online (Sandbox Code Playgroud)

如何循环@attributes来显示数据?

foreach ($xml as …
Run Code Online (Sandbox Code Playgroud)

php

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

回调函数返回 return($var &amp; 1)?

我已经阅读了关于array_filter的 PHP手册

<?php
function odd($var)
{
    // returns whether the input integer is odd
    return($var & 1);
}

function even($var)
{
    // returns whether the input integer is even
    return(!($var & 1));
}

$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);

echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
?>
Run Code Online (Sandbox Code Playgroud)

即使我在这里看到结果:

Odd :
Array
(
    [a] => 1
    [c] => 3
    [e] => 5
)
Even:
Array
( …
Run Code Online (Sandbox Code Playgroud)

php bitwise-operators

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

字符串替换Python中的元音?

预期:

>>> removeVowels('apple')
"ppl"
>>> removeVowels('Apple')
"ppl"
>>> removeVowels('Banana')
'Bnn'
Run Code Online (Sandbox Code Playgroud)

代码(初学者):

def removeVowels(word):
    vowels = ('a', 'e', 'i', 'o', 'u')
    for c in word:
        if c in vowels:
            res = word.replace(c,"")
    return res 
Run Code Online (Sandbox Code Playgroud)

我如何小写和大写?

python

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

什么样的日期格式?

我获得了源数据,并在其源代码中包含此格式的日期

2011-01-12T14:41:35.7042252+01:00

我不知道他们使用的日期格式是什么.

php datetime

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

哎呀.conf/routes或conf/application.conf缺失.?

我在官方网站上关注了" 学习 ".我正在使用Windows 7 + Chrome.

  D:\tester\play>dir
 Volume in drive D is APPLIS
 Volume Serial Number is 9037-F7BD

 Directory of D:\tester\play

07/15/2011  10:38 PM    <DIR>          .
07/15/2011  10:38 PM    <DIR>          ..
06/15/2011  11:00 AM             1,810 COPYING
07/15/2011  10:33 PM    <DIR>          documentation
07/15/2011  10:34 PM    <DIR>          framework
07/15/2011  10:35 PM    <DIR>          modules
06/06/2011  08:10 PM             5,858 play
06/06/2011  08:10 PM                50 play.bat
07/15/2011  10:35 PM    <DIR>          python
06/06/2011  08:10 PM             1,834 README.textile
07/15/2011  10:35 PM    <DIR>          resources
07/15/2011  10:33 PM    <DIR> …
Run Code Online (Sandbox Code Playgroud)

java playframework

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

列表反向,列出多个-1

预期结果:

>>> createLists(5)
([1, 2, 3, 4, 5], [5, 4, 3, 2, 1], [ -5, -4, -3, -2, -1], [-1, -2, -3, -4, -5])
Run Code Online (Sandbox Code Playgroud)

我的代码:

  # Write a function that returns 4 lists given a postive number.
    def createLists(num): 
        ls =    [item for item in range(num)]
        return set(ls, ls.reverse(), ls.reverse()*(-1), ls*(-1))
Run Code Online (Sandbox Code Playgroud)

有人指导我怎么做吗?

python

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