From what I understand you want to implement a script that is able to display only the first of the ordered pair contained in each sublists of a particular list.
Well, to implement a code that is able to accomplish such a task, you must be aware that you must iterate on the list outermost and also the more internal lists - the sublists.
To resolve this issue we can use the following code:
lista = [[2, 5], [3, 6], [4, 7], [5, 8]]
for lis in lista:
for c in lis:
if c == lis[0]:
print(c)
Note that when we execute the following code the first time will begin to iterate on the most external list, that is, on the list called lista
. Continuing the implementation, the second for will begin to iterate on each one internal lists - sub-lists - and, with the help of the block if will be verified if the value of the respective iteration belongs to the first element of the ordered pair - first element of the sublist, ie the element corresponding to the position lis[0]
. If yes, it is displayed on the screen.
Note that the results of this code will be displayed in a column.
Now if you want to display these values on the same line, just use the following code:
lista = [[2, 5], [3, 6], [4, 7], [5, 8]]
for lis in lista:
for c in lis:
if c == lis[0]:
print(c, end=' ')
print()
Note that these two codes is able to work with only one list, which is precisely the list I wrote inside the code.
Now imagine the following situation: "Create a list of 6 sublists, where each contains an ordered pair - (x, f(x)) - where "x"
be an integer value and "f(x)"
be the square of "x"
.
How could we resolve this situation?
To resolve this situation we must:
- Insert each of the
6
values - one at a time;
- Assemble the list containing the
6
sublists, each with their respective ordered pair;
- Display the list;
- Capture only the "x" components of each ordered pair and display them.
Following this logic we can use the following code:
# Primeiro bloco for:
lista = list()
for i in range(1, 7):
par_ordenado = list()
for j in range(1, 3):
if j == 1:
par_ordenado.append(int(input(f'Digite o valor do {i}º "X": ')))
else:
par_ordenado.append(par_ordenado[0] ** 2)
lista.append(par_ordenado)
print(f'A lista montada é:\n{lista}')
# Segundo bloco for:
print('As componentes "x" do pares ordenados são:')
for lis in lista:
for c in lis:
if c == lis[0]:
print(c, end=' ')
print()
Observing
If you want to display the "f(x)" components of for ordered, just go to 2nd block for and replace the command line...
if c == lis[0]
for...
if c == lis[1]
Affz, it was faster than I kkk When I was almost done answering appeared that you had already answered.
– JeanExtreme002
It worked out here, vlw!
– Feitosafelipe