2012-12-24 32 views
5

我是一個相當舒適的PHP程序員,並且擁有很少的Python經驗。我試圖幫助他的項目夥伴,代碼很容易寫入Php,我大部分都移植過了,但如果可能的話,需要一些幫助來完成翻譯。 目標是:從PhP到Python的飛躍

  • 生成基本對象與UID的
  • 隨機選擇一些項目,創建鍵入包含新 性質的UID第二列表的列表。
  • 測試兩個列表之間的交集以相應地改變響應。

下面是什麼,我想代碼在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)是什麼,我想用它來做一個列表工作作爲副教授陣列

編輯:小幅調整。

+0

嘗試使用'dirtyItems'爲'dict',而不是'list'。你可以插入一個鍵值對作爲'dirtyItems [key] = value' – Netro

+0

這真是太好了,我現在開始工作了,我覺得很搞笑,基本上變化的範圍包括**切換[]到{} **和其他一些小的更正。現在我可以將它集成到下一版本的主項目中。 – Toaster

+0

如果你正在做相交集等事情,那麼你應該製作對象[hashable](http://docs.python.org/2/reference/datamodel.html#object.__hash__),以便他們可以參與' set'。 –

回答

2

您聲明dirtyItems爲數組而不是字典。在Python中,它們是不同的類型。

改爲dirtyItems = {}

+0

謝謝裝載傢伙!我可以感覺到它很接近,我不知道要花多長時間才能找出字典類型。 – Toaster

1

做一個字典而不是數組:

import random 
import math 

dirtyItems = {} 

然後你可以使用這樣的:

dirtyItems[key.uid] = x