标签: coding-style

Python:优雅并保存编码方式,创建多个列表

通常为了将结果存储在python中的几个列表中,我在循环之前创建相应的空列表.

A = []
B = []
c = []
D = []
E = []
F = []

for i in range(100):
  # do some stuff
Run Code Online (Sandbox Code Playgroud)

我有一种方法在单个代码行(或少数)中创建列表?

python optimization coding-style list

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

循环数组,更好,更快等等?

我想实现一个Hash或类似PHP的数组.什么是更好的,选项a)或选项b)通过其键找到元素?

(所有变量都已设置并初始化,等等!)

一个)

for( i = 0; i < ary->element_cnt && found == NULL; i++ ) {
    current_element = &(ary->elements[i]);
    if( 0 == memcmp(current_element->key, search_key, keysize) ) {
        found = current_element;
    }
}
Run Code Online (Sandbox Code Playgroud)

b)

for( i = 0, current_element = &(ary->elements[i]) ; 
        i < ary->element_cnt &&  
        0 != memcmp(current_element->key, searchkey, keysize); 
        i++, current_element = &(ary->elements[i]) );
/*found = current_element;*/
Run Code Online (Sandbox Code Playgroud)

第一个更好,因为它更易读/可维护吗?第二个会更快吗?

在一个大循环中做所有事情是"糟糕的风格"吗?

我知道,那里有更好的搜索算法,但这不是我的问题!

c arrays algorithm search coding-style

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

在Java代码约定中,实例变量应该放在方法之前还是放在方法之后?

在Java代码约定中,实例变量应该放在方法之前还是放在方法之后?我知道,在Oracle或Apache的代码约定中,他们建议应该在方法之前放置实例变量.但是,在Core Java一书中,作者将实例变量放在方法之后.嗯......也许是因为我的C++背景,在我学习C++的过程中,我了解到公共函数应放在私有成员之前,因为人们更关心代码提供的功能.因此,我想知道哪种方式首选?如果你能用你的实际项目经验解释你的理由,那就更好了.

java coding-style

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

"by bytes"vs"in bytes"

template<class CharType>
struct StringWithLength
{
    size_t    length;
    CharType* str_buf;
};
Run Code Online (Sandbox Code Playgroud)

我想在字段长度上添加一些注释.我有两个选择:

#1. "The field length is the size of str_buf by the byte" 
    (Consider "The worker is paid by the hour")

#2. "The field length is the size of str_buf in bytes"
Run Code Online (Sandbox Code Playgroud)

从英语母语人士的角度来看哪个更自然?

谢谢.

c++ comments coding-style

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

python:优雅和代码保存方式转换列表

首先抱歉这个简单的问题.我有一个清单(只是一个例子)

points = [[663963.7405329756, 6178165.692240637],
 [664101.4213951868, 6177971.251818423],
 [664099.7474887948, 6177963.323432223],
 [664041.432877932, 6177903.295650704],
 [664031.8017317944, 6177895.797176996],
 [663963.7405329756, 6178165.692240637]]
Run Code Online (Sandbox Code Playgroud)

我需要将其转换为以下形式

points = [(663963.7405329756, 6178165.692240637),
 (664101.4213951868, 6177971.251818423),
 (664099.7474887948, 6177963.323432223),
 (664041.432877932, 6177903.295650704),
 (664031.8017317944, 6177895.797176996),
 (663963.7405329756, 6178165.692240637)]
Run Code Online (Sandbox Code Playgroud)

为了Polygon使用shapely模块创建一个对象.我写了几个循环,但真的不优雅和耗时.你知道将第一个列表转换成第二个列表的最佳方法吗?

谢谢

python optimization performance coding-style

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

在python中写if else的更优雅方式

例如,如果我有代码:

class Example ():

    def a(self):
        return 'Buy'

    def b(self):
        if (self.a() == 'Buy'):
            return 'BUY SET'
        elif (self.a() == 'Sell'):
            return 'SELL SET''

o = Example()
value = o.b()   
print value 
Run Code Online (Sandbox Code Playgroud)

是否有更优雅/ pythonic的方式我可以写函数b(自我)?

python coding-style

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

为什么在开发PHP脚本时使用/ r/n?

在互联网上到处寻找答案,没有太多运气,所以我在这里.我一直在解析从CodeCanyon.net网站上购买的脚本以学习OOP,我注意到很多行以/ r/n结尾.为什么这是常见的做法,实际用途是什么?提前致谢!

php coding-style

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

在Ruby 1.9中有一个使用句子":foo =>'bar'"或"foo:'bar'"的约定?

是否有使用Ruby 1.9+ =>:在Ruby 1.9+中使用的约定?喜欢:

:param => "foo"
Run Code Online (Sandbox Code Playgroud)

要么

param: "foo"
Run Code Online (Sandbox Code Playgroud)

编辑:感谢您的反馈,我编辑了这个问题,使其更加清晰.

ruby coding-style

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

srand函数返回相同的值

嘿伙计们来看看这个节目.

/* The craps game, KN king page 218 */

#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>

int roll_dice(void);
bool play_game(void);

int roll_dice(void)
{
    int roll;

    getchar();
    srand((unsigned) time(NULL));

    roll = rand() % 13;

    if(roll == 0)
    roll = roll + 1;

    return roll;
}

bool play_game()
{
    int sum = 0, wins = 0, loss = 0, point;

    sum = roll_dice();

    printf("You rolled: %d", sum);

    if(sum == 7 || sum == 11)
    {
        printf("\nYou won!\n");
        return true;
    } …
Run Code Online (Sandbox Code Playgroud)

c coding-style srand

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

写这些支票的正确方法?

写这些支票的正确方法是什么.我有一个数组,一些时间值没有设置或它们是空的.现在代码有点难以阅读,我该怎么做才能让它看起来更清晰,更可靠.或者我只是在努力.有什么建议?

<?php 

private static function filter_yt_data($yt_api_response = null)
    {
        $data     =    array(); 
        $api      =    json_decode($yt_api_response);
        $api      =    $api->data->items[0];



        $data = array(
                    'id'            =>   $api->id,
                    'author'        =>   e($api->uploader),
                    'label'         =>   e($api->category),
                    'title'         =>   e($api->title),
                    'description'   =>   e($api->description),
                    'duration'      =>   $api->duration,
                    'view_count'    =>   $api->viewCount,
                    'access'        =>   $api->accessControl->embed,
                    'favorite_count'=>   $api->favoriteCount
                    );

        if($api->accessControl->comment == 'allowed')
        {
           $data['comment_count']   =   $api->commentCount;
        }
        else
        {
            $data['comment_count']  =   0;
        }

        if($api->ratingCount)
        {
            $data['rating_count']  =   $api->ratingCount;
        }
        else
        {
            $data['rating_count']  =   0;
        }

        if($api->accessControl->rate == 'allowed')
        {
            $data['like_count'] = …
Run Code Online (Sandbox Code Playgroud)

php coding-style

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

标签 统计

coding-style ×10

python ×3

c ×2

optimization ×2

php ×2

algorithm ×1

arrays ×1

c++ ×1

comments ×1

java ×1

list ×1

performance ×1

ruby ×1

search ×1

srand ×1