Python Forum
Correspondence of values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Correspondence of values (/thread-7504.html)

Pages: 1 2


Correspondence of values - William049 - Jan-13-2018

Hello I'm new:), I'm on the realisation of a family tree project, it is 95% complete (approximately 200 lines) But I can't do something that I think is simple.

I currently have a function that has the end of its process returns to me a table of this form, which may be longer in some cases but the syntax is the same:
[0, 1]
I have a list of people that is built as the user's choices of the form grow:
liste = [[' NAME1',' SURNAME1'],[' NAME2',' SURNAME2'],[' NAME3',' SURNAME3']]
I would like from the value table[0,1], it returns to me the first name of the corresponding person in the list of persons SURNAME

The function who return me the table of value is it:
def descendants(num3):
    descendants = []
    for i in range(len(listeparentes)):
        if (num3 == listeparentes[i][0]):
            descendants.append(listeparentes[i][1])
    return (descendants)
For example, with the values listed in the table, the program must return me
SURNAME1
SURNAME2
I was thinking about something like that.. but that's not correct..
def ValueToSurname(name):
	name = []
	for i in descendants:
		name.append(liste[i][1])
	return (name)
Thanks if you could help me :)


RE: Correspondence of values - Gribouillis - Jan-13-2018

If you want the lists of surnames, you can do
liste = [[' NAME1',' SURNAME1'],[' NAME2',' SURNAME2'],[' NAME3',' SURNAME3']]
surnames = [t[1] for t in liste]



RE: Correspondence of values - William049 - Jan-13-2018

Thanks for the answer but that not respond at what i need.
There is not table of value so...

Quote:I would like from the value table[0,1], it returns to me the first name of the corresponding person in the list of persons SURNAME



RE: Correspondence of values - Gribouillis - Jan-13-2018

I don't understand the question. Could you give a complete example of input and expected output and what is the link between the given input and the expected output ?


RE: Correspondence of values - William049 - Jan-13-2018

There is the complete example in my first post.

I have a list :
liste = [[' NAME1',' SURNAME1'],[' NAME2',' SURNAME2'],[' NAME3',' SURNAME3']]
I get the output of one of my functions :
def descendants(num3):
    descendants = []
    for i in range(len(listeparentes)):
        if (num3 == listeparentes[i][0]):
            descendants.append(listeparentes[i][1])
    return (descendants)
The output for example is that :
[0, 1]
Now I want to reconcile the table of values and the list of values.
So the output should be :

SURNAME1
SURNAME2



RE: Correspondence of values - Gribouillis - Jan-13-2018

Why SURNAME1 and SURNAME2 and not SURNAME3? How can the function return [0, 1]? What is the meaning, if any, of the 0 and the 1 ?


RE: Correspondence of values - William049 - Jan-13-2018

The indice 0 and 1 cannot correspond to the SURNAME3 ...

0 = SURNAME1
1 = SURNAME2
My function return a list of index of brothers and sisters, but i want to return the SURNAME NOT THE INDEX xD

def ascendant(num3):
    ascendants = []
    for i in range(len(listeparentes)):
        if (num3 == listeparentes[i][1]):
            ascendants.append(listeparentes[i][0])
    return (ascendants)
So i need to create a function that will take the return of the function "ascendants" and will return the list of surnames of people


RE: Correspondence of values - Gribouillis - Jan-13-2018

This one should work:
def function(laliste, lesindices):
    return [laliste[i][1] for i in lesindices]

print(function(liste, [0, 1]))
Can you give example of num3, listeparentes in ascendant()?


RE: Correspondence of values - William049 - Jan-13-2018

You're very close to good code.

Look a screen of my program that will be the more simple :) : http://prntscr.com/hzyrhn

PS : Error in screen that's not "Les ascendants connus de cette personne sont [0]"
but " Les descendants connus de cette personne sont [0]" , that change nothing for the code :)


EDIT : That work !
http://prntscr.com/hzyuax
The new code :

def function(laliste, lesindices):
    return [laliste[i][1] for i in lesindices]
print(function(liste, [u]descendants(num1)))[/u]

I'm trying to delete ["] of the return of the function you have created, so i'm using my code :
def affichage():
    for i in range(len(listepersonnes)):
        for j in range(len(listepersonnes[i])):
            print(listepersonnes[i][j], end=" ")
        print()
But my code doens't take argument..
It's possible to modify your function in order to create a new list ?
If it's possible i will be able to use my code who's working :)


RE: Correspondence of values - Gribouillis - Jan-13-2018

Try
print('\n'.join(function(liste, descendants(num1))))
This is based on this example
>>> L = ['spam', 'eggs', 'ham']
>>> print('\n'.join(L))
spam
eggs
ham
Can you post the whole code ?