简体   繁体   中英

Need help finding and indexing a sequence of entries in a list Python

I am looking to find a sequence of entries within a specific list and then find it's location so that I may add some extra entries on the sequence after a specific point. I need to find this out as I'm working on a Grid-based board for a turn based game that I need to store data in at any time necessary.

My code is currently:

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")

Main File:

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

I want to be able to take a list of multiple entries of

[x location, z location, whatever, whatever end, x location, z location, whatever end, x location, z location, whatever, whatever, whatever end, etc]

and then add a whatever into right after where x location is for example, 32 and z is 50. So it'd end up being

(... 32, 50, whatever, end)

It might be beneficial to use a dict for this sort of thing. You could for example use the global Tiles and its indexes to access a particular tile.

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"}
        ]

So for example:

>>> 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'})

And then you could update the items, add to them, or copy from another tile:

>>> 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}

To find a sequence of entries will depend on what the grid dimensions are.

In my example I am using a 1x3.

So to find a series I will create a 3x3 and then use an index range.

>>> 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'}]]

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM