5

私はかなり快適な PHP プログラマーであり、Python の経験はほとんどありません。私は彼のプロジェクトで仲間を助けようとしています.コードはPHPで書くのに十分簡単です.私はそのほとんどを移植しましたが、可能であれば翻訳を完了するのに少し助けが必要です. 目標は次のとおりです。

  • uid を持つ基本オブジェクトのリストを生成する
  • いくつかの項目をランダムに選択して、新しいプロパティを含む uid をキーとする 2 番目のリストを作成します。
  • それに応じて応答を変更するために、2 つのリスト間の交差をテストします。

以下は、私がPythonでコーディングしようとしているものの実際の例です

<?php
srand(3234);
class Object{  // Basic item description
    public $x       =null;
    public $y       =null;
    public $name    =null;
    public $uid     =null;
}
class Trace{  // Used to update status or move position
#   public $x       =null;
#   public $y       =null;
#   public $floor   =null;
    public $display =null;  // Currently all we care about is controlling display
}
##########################################################
$objects = array();
$dirtyItems = array();

#CREATION OF ITEMS########################################
for($i = 0; $i < 10; $i++){
    $objects[] = new Object();
    $objects[$i]->uid   = rand();
    $objects[$i]->x     = rand(1,30);
    $objects[$i]->y     = rand(1,30);
    $objects[$i]->name  = "Item$i";
}
##########################################################

#RANDOM ITEM REMOVAL######################################
foreach( $objects as $item )
    if( rand(1,10) <= 2 ){  // Simulate full code with 20% chance to remove an item.
        $derp = new Trace();
        $derp->display = false;
        $dirtyItems[$item->uid] = $derp;  //#  <- THIS IS WHERE I NEED THE PYTHON HELP
        }
##########################################################
display();

function display(){
global $objects, $dirtyItems;
    foreach( $objects as $key => $value ){  // Iterate object list
        if( @is_null($dirtyItems[$value->uid]) )  // Print description
            echo "<br />$value->name is at ($value->x, $value->y) ";
        else  // or Skip if on second list.
            echo "<br />Player took item $value->uid";

    }
}
?>

したがって、実際にはほとんどがソートされていますが、Python のバージョンの連想配列に問題があり、キーがメイン リストのアイテムの一意の数と一致するリストを作成するのに問題があります。

上記のコードからの出力は、次のようになります。

Player took item 27955
Player took item 20718
Player took item 10277
Item3 is at (8, 4) 
Item4 is at (11, 13)
Item5 is at (3, 15)
Item6 is at (20, 5)
Item7 is at (24, 25)
Item8 is at (12, 13)
Player took item 30326

私の Python スキルはまだ中途半端ですが、これは上記とほぼ同じコード ブロックです。リスト関数 .insert( ) または .setitem( ) を調べて使用しようとしましたが、期待どおりに機能していません。

これは私の現在の Python コードで、まだ完全には機能していません

import random
import math

# Begin New Globals
dirtyItems = {}         # This is where we store the object info
class SimpleClass:      # This is what we store the object info as
    pass
# End New Globals

# Existing deffinitions
objects = []
class Object:
    def __init__(self,x,y,name,uid):
        self.x = x  # X and Y positioning
        self.y = y  #
        self.name = name #What will display on a 'look' command.
        self.uid = uid

def do_items():
    global dirtyItems, objects
    for count in xrange(10):
        X=random.randrange(1,20)
        Y=random.randrange(1,20)
        UID = int(math.floor(random.random()*10000))
        item = Object(X,Y,'Item'+str(count),UID)
        try: #This is the new part, we defined the item, now we see if the player has moved it
            if dirtyItems[UID]:
                print 'Player took ', UID
        except KeyError:
            objects.append(item) # Back to existing code after this
            pass    # Any error generated attempting to access means that the item is untouched by the player.

# place_items( )
random.seed(1234)

do_items()

for key in objects:
    print "%s at %s %s." % (key.name, key.x, key.y)
    if random.randint(1, 10) <= 1:
        print key.name, 'should be missing below'
        x = SimpleClass()
        x.display = False
        dirtyItems[key.uid]=x

print ' '
objects = []
random.seed(1234)

do_items()

for key in objects:
    print "%s at %s %s." % (key.name, key.x, key.y)

print 'Done.'

長い投稿で申し訳ありませんが、両方の完全なコード セットを提供したいと思います。PhP は完全に機能し、Python も近いです。誰かが私を正しい方向に向けることができれば、それは大きな助けになるでしょう. dirtyItems.insert(key.uid,x)は、リストをAssoc配列として機能させるために使用しようとしたものです

追記:微修正。

4

2 に答える 2

1

配列の代わりに辞書を作成します。

import random
import math

dirtyItems = {}

次に、次のように使用できます。

dirtyItems[key.uid] = x
于 2012-12-24T07:17:17.997 に答える