Jump to content

Extrair valores de campos em dicionários para uma lista é possível, ou tenho de iterar ?


jonhhy

Recommended Posts

Bom dia membros da comunidade P@P,

 

tenho uma string de nome, para identificar o grupo que quero destacar, contudo se houvesse forma de extrair só o nome das mulheres (diretamente era muito menos trabalhoso)... como é python penso sempre que deve haver alguma maneira melhor... indo ao exercício e código:

# Desafio 094


 

Crie um programa que leia **nome**, **sexo** e **idade** de **várias pessoas**, guardando os dados de cada pessoa em um **dicionário** e todos os dicionários em uma **lista**. No final, mostre:

 

**A)** Quantas pessoas foram cadastradas 

 

**B)** A **média** de idade do grupo.

 

**C)** Uma lista com todas as **mulheres**.

 

**D)** Uma lista com todas as pessoas com **idade** acima da **média**.

 

signup = {'name': '', 'gender': '', 'age': ''}
people = []
women = []
oldpeople = []
adelante = ''
agesum = 0
while True:
  signup['name'] = str(input('Say person name: '))
  signup['gender'] = str(input('Say person gender: '))
  while signup['gender'] not in 'MmFf':
    signup['gender'] = str(input('We only consider gender: [F/M] ... : '))
  signup['age'] = int(input('Say your age: '))
  while signup['age'] < 0:
    signup['age'] = int(input('Age cannot be negative, say a positive number with the years: '))
  agesum += signup['age']
  people.append(signup.copy())
  if signup['gender'].strip().upper() in 'F':
    #women.append(signup['name'].copy())
    women.append(signup.copy())
  #signup.clear()
  adelante = str(input('Did you want continue to sign up person? :'))
  if adelante in 'Nn':
    break
print(f" People signup was {len(people)}")
print(f" Age mean is {agesum/len(people)}")
print(f" Women are {women[0]}")


for person in people:
  if person['age'] > (agesum/len(people)):
    oldpeople.append(person['name'])

print(f" Person with more that mean age is {oldpeople}")
Say person name: Luísa
Say person gender: f
Say your age: 26
Did you want continue to sign up person? :  yes
Say person name: Miguel
Say person gender: 31
We only consider gender: [F/M] ... :  mas
We only consider gender: [F/M] ... : m
Say your age: 31
Did you want continue to sign up person? :   Yieh
Say person name: Sónia
Say person gender: 11
We only consider gender: [F/M] ... : f
Say your age: 11
Did you want continue to sign up person? :n
 People signup was 3
 Age mean is 22.666666666666668
 Women are {'name': 'Luísa', 'gender': 'f', 'age': 26}
 Person with more that mean age is ['Luísa', 'Miguel']

a dúvida é no primeiro comentário do código, que dá erro..

Edited by jonhhy
Link to comment
Share on other sites

Resolvido:

 

signup = {'name': '', 'gender': '', 'age': ''}
people = []
women = []
oldpeople = []
adelante = ''
agesum = 0
while True:
  signup['name'] = str(input('Say person name: '))
  signup['gender'] = str(input('Say person gender: '))
  while signup['gender'] not in 'MmFf':
    signup['gender'] = str(input('We only consider gender: [F/M] ... : '))
  signup['age'] = int(input('Say your age: '))
  while signup['age'] < 0:
    signup['age'] = int(input('Age cannot be negative, say a positive number with the years: '))
  agesum += signup['age']
  people.append(signup.copy())
  if signup['gender'].strip().upper() in 'F':
    women.append(signup['name'])
    #women.append(signup.copy())
  #signup.clear()
  adelante = str(input('Did you want continue to sign up person? :'))
  if adelante in 'Nn':
    break
print(f" People signup was {len(people)}")
print(f" Age mean is {agesum/len(people)}")
print(f" Women are : {women}")


for person in people:
  if person['age'] > (agesum/len(people)):
    oldpeople.append(person['name'])

print(f" Person with more that mean age is {oldpeople}")

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.