寻找并索引Python列表中的一系列条目。

2024-06-16 08:42:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望在一个特定的列表中找到一个条目序列,然后找到它的位置,这样我就可以在一个特定点之后的序列中添加一些额外的条目。我需要找出这一点,因为我正在为一个基于回合的游戏,我需要在任何必要的时候存储数据网格板工作

我的代码当前为:

CreateWorld.py:

global BaseHealth
BaseHealth = 10

global BaseHappiness
BaseHappiness = 10

global BaseIntelligence
BaseIntelligence = 10

global BaseProduction
BaseProduction = 10

global BaseWealth
BaseWealth = 10

global Tiles
Tiles = []


#The Process of Generating a Blank Map xAxis long and zAxis wide
def createLandscape(xAxis, zAxis):
    #Sets the amount of createdRows to 0
    createdRows = 0

    #Calls createRow() the amount of times that the Map is Long
    while createdRows < xAxis:
        createRow(createdRows, zAxis)
        createdRows += 1

#The Process of Generating a Blank Map Row zAxis Wide and with a Row Number of RowsCreated
def createRow(RowsCreated, zAxis):
    #Sets the amount of createdTiles to 0
    createdTiles = 0
    createdRows = RowsCreated

    #Calls createTile() the amount of times that the Row is wide
    while createdTiles < zAxis:
        createTile(createdRows, createdTiles)
        createdTiles += 1

#The Process of Generating a Blank Map Tile in the 
def createTile(RowsCreated, TilesCreated):
    global Tiles
    Tiles.append(RowsCreated)
    Tiles.append(TilesCreated)
    Tiles.append("end")

主文件:

import sys

sys.path.insert(1,'/BaseGame')

from Startup import Start
from Startup import MainMenu
from BaseGame import CreateWorld

global Tiles


#Start.start()
#MainMenu.listMenu_Main00()
CreateWorld.createLandscape(3, 3)


Tiles.insert()
print Tiles

我想能够采取一个列表的多个条目的

[x位置,z位置,任意,任意端,x位置,z位置,任意端,x位置,z位置,任意,任意,任意,任意端,等等]

然后在x的位置后面加上一个whatever,例如,32,z是50,这样就得到了

(。。。32,50,随便什么,结束)


Tags: oftheimportmapprocessamountglobaltiles
1条回答
网友
1楼 · 发布于 2024-06-16 08:42:36

对这类事情使用dict可能是有益的。例如,您可以使用全局Tiles及其索引来访问特定的磁贴

Tiles = [{'x_loc':32, 'z_loc':50, 'stuff1':"stuff1", 'stuff2':"stuff2"},
         {'x_loc':33, 'z_loc':51, 'stuff3':"stuff3", 'stuff4':"stuff4"},
         {'x_loc':34, 'z_loc':52, 'stuff5':"stuff5", 'stuff6':"stuff6"}
        ]

例如:

>>> for id, item in enumerate(Tiles):print(Tiles[id]['x_loc'])
... 
32
33
34
>>> for id, item in enumerate(Tiles):print(Tiles[id]['x_loc'], item)
... 
(32, {'z_loc': 50, 'x_loc': 32, 'stuff1': 'stuff1', 'stuff2': 'stuff2'})
(33, {'z_loc': 51, 'stuff4': 'stuff4', 'x_loc': 33, 'stuff3': 'stuff3'})
(34, {'z_loc': 52, 'stuff5': 'stuff5', 'x_loc': 34, 'stuff6': 'stuff6'})

然后可以更新项目、添加到项目或从其他磁贴复制:

>>> Tiles[1]
{'z_loc': 51, 'stuff4': 'stuff4', 'x_loc': 33, 'stuff3': 'stuff3'}
>>> Tiles[1].update({'stuff4':'stuff4updated'})
>>> Tiles[1]
{'z_loc': 51, 'stuff4': 'stuff4updated', 'x_loc': 33, 'stuff3': 'stuff3'}
>>> Tiles[1].update({'stuff4':'stuff4updatedtwice', 'stuff3':'stuff3updated'})
>>> Tiles[1]
{'z_loc': 51, 'stuff4': 'stuff4updatedtwice', 'stuff3': 'stuff3updated', 'x_loc': 33}
>>> Tiles[1]
{'z_loc': 51, 'stuff4': 'stuff4updatedtwice', 'stuff3': 'stuff3updated', 'x_loc': 33}
>>> Tiles[1].update({'stuff2':Tiles[0]['stuff2']})
>>> Tiles[0]
{'z_loc': 50, 'x_loc': 32, 'stuff1': 'stuff1', 'stuff2': 'stuff2'}
>>> Tiles[1]
{'z_loc': 51, 'stuff4': 'stuff4updatedtwice', 'stuff2': 'stuff2', 'stuff3': 'stuff3updated', 'x_loc': 33}

查找条目序列将取决于网格尺寸

在我的例子中,我使用的是1x3

为了找到一个系列,我将创建一个3x3,然后使用一个索引范围

>>> Tiles2 = [Tiles,Tiles,Tiles]
>>> Tiles2[0][0:2]
[{'z_loc': 50, 'x_loc': 32, 'stuff1': 'stuff1', 'stuff2': 'stuff2'}, {'z_loc': 51, 'stuff4': 'stuff4updatedtwice', 'stuff2': 'stuff2', 'stuff3': 'stuff3updated', 'x_loc': 33}]
>>> Tiles2[0:1][0:2]
[[{'z_loc': 50, 'x_loc': 32, 'stuff1': 'stuff1', 'stuff2': 'stuff2'}, {'z_loc': 51, 'stuff4': 'stuff4updatedtwice', 'stuff2': 'stuff2', 'stuff3': 'stuff3updated', 'x_loc': 33}, {'z_loc': 52, 'stuff5': 'stuff5', 'x_loc': 34, 'stuff6': 'stuff6'}]]

`

相关问题 更多 >