Selective and Organized Information in Python

Asked

Viewed 36 times

-1

I have two lists that send me in real time (ie every second) information if the player is online or offline.

def online(nome):
   # aqui será armazenado o nome da pessoa que está online

def offline(nome):
   # aqui será armazenado a pessoa que está offline

I wish there was a way to store both values but show first those that are online and only then offline:

OUTPUT:
Nome A do jogador online
Nome B do jogador online
Nome C do jogador online
Nome A do jogador offline
Nome B do jogador offline

I know it’s for append if you want to make a list and update if you want to do it by a dictionary, but the problem is that the information sends one on top of the other and I need you to study on a single list/dictionary/tuple/ whatever you think is best for my problem!

1 answer

2


There are some options. But, let’s go in parts.

Thinking about the structure

There are several players and they can be online or offline. Thinking only of this information the structure that comes to mind initially is the list, but list of what?

Option 1

List of online players and offline users.

Access to the data is simple using a for, but in case the user changes the status from online to offline or vice versa, although not complex, requires the removal of a list and inclusion in another

Example:

jogadores_online = ["Pedro", "Joao"...]
jogadores_offline = ["Jose", "Maria"...]

Option 2

List of player tuples containing their status.

In a single list you have all players, simple to access. However, being the immutable tuples, in a status change, would have to remove the item from the list and re-emerge with a new status.

# True para jogador online e False para offline
jogadores = [ ("Pedro", True), ("Joao", True), ("Jose", False), ("Maria", False)]

To access this list, you can browse it using for or use the filter

Example 1: List of players

nomes_jogadores = []
for j in jogadores:
    nomes_jogadores.append(j[0])

or

nomes_jogadores = [j[0] for j in jogadores]

Example: Online player list

nomes_jogadores_online = []
for j in jogadores:
    if j[1]:
        nomes_jogadores_online.append(j[0])

print(nomes_jogadores_online)
['Pedro', 'Joao']

or

jogadores_online = filter(lambda j: j[1], jogadores)

print(list(jogadores_online))
[('Pedro', True), ('Joao', True)]

Note: the filter in Python 3 does not return a list. It returns an iterable and can be accessed with for.

or explicitly

def is_online(j):
    return j[1]

jogadores_online = filter(is_online, jogadores)

print(list(jogadores_online))
[('Pedro', True), ('Joao', True)]

Note: Note that using the filter it returns the tuple and not only the names. If you only want the names, use the map together with the filter as below:

jogadores_online = map(lambda x: x[0], filter(is_online, jogadores))

print(list(jogadores_online))
['Pedro', 'Joao']

Note: The map also returns eternal

Option 2.5

List of player lists containing their status.

# True para jogador online e False para offline
jogadores = [ ["Pedro", True], ["Joao", True], ["Jose", False], ["Maria", False] ]

The advantage over Option 2 is that the list is changeable, so you can change the player’s status

# Mudando o status do Pedro
jogadores[0][1] = False

Option 3

Object list.

class Jogador:
    def __init__(self, nome, status):
        self.nome = nome
        self.status = status

jogadores = []
jogadores.append(Jogador("Joao", True))
jogadores.append(Jogador("Pedro", True))
jogadores.append(Jogador("Jose", False))
jogadores.append(Jogador("Maria", False))

In case the player needs more attributes it is easy to modify (this would be against for option 2 and 2.5)

The functions of filter and map can be used in this structure since objects are in a list

jogadores_online = filter(lambda j: j.status, jogadores)

Option 4

List of dictionaries

jogadores = [{"nome": "Joao", "status": True}, {"nome": "Pedro", "status": True}, {"nome": "Jose", "status": False }, {"nome": "Maria", "status": False}]

In a simple structure, it makes sense. The functions filter and map can be used

Option 5

Running from the lists we have the Dictionary

jogadores = {
             "Joao": True,
             "Pedro": True,
             "Jose": False,
             "Maria": False
            }

Considering that each player has a unique name, this is the easiest solution as you can access the player directly by name and check their status. The function filter can be used if you access jogadores.items()

print(jogadores.items())
dict_items([('Joao', True), ('Pedro', True), ('Jose', False), ('Maria', False)])

Option N

List of namedtuple (I won’t go into details) For more information see here

from collections import namedtuple

Now the decision is from the implementer

I hope I’ve helped

  • Thank you very much!!

  • Cool too! + 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.