R
R
rosh1k2022-02-05 11:43:20
Python
rosh1k, 2022-02-05 11:43:20

How to display only certain elements of an array?

There is a code that calculates the seating:

import os
import csv
from tkinter import *
import tkinter as tk
import pandas as pd




#переменные 
gh = int(0)
rh = int(0)
rm = int(0)
sh = int(0)
mm = int(0)
def close_window():
    global n_desk1
    global n_ryadov1
    global n_file
    n_desk1 = n_desk_entry.get()
    n_file = n_file_entry.get()
    n_ryadov1 = n_ryadov_entry.get()
    root.destroy()
root = Tk()
root.title('Расчет рассадки')
root.geometry('600x400+200+100')
n_ryadov_label = Label(root, text = 'Введите кол-во рядов', )
n_ryadov_label.pack(anchor = CENTER)
n_ryadov_entry = Entry(root)
n_ryadov_entry.pack(anchor = CENTER)
n_desk_label = Label(root, text = 'Введите кол-во учеников за партой')
n_desk_label.pack()
n_desk_entry = Entry(root)
n_desk_entry.pack(anchor = CENTER)
n_file_label = Label(root, text = 'Введите название файла')
n_file_label.pack(anchor = CENTER)
n_file_entry = Entry(root)
n_file_entry.pack(anchor = CENTER)
Button = Button(root, text = "Начать" , command =close_window)
Button.pack(anchor = S)
root.mainloop()
n_desk = int(n_desk1)
n_ryadov = int(n_ryadov1)




with open (n_file , 'r')  as f:
    a = csv.reader(f, delimiter = ';')
    s = 0
    students = []
    for i in a:
        s+=1
        name , surname , growth, zrenie = i
        growth = float(growth)
        zrenie = float(zrenie)
        if zrenie > 1:
            zrenie = 2 - zrenie
        students.append((name, surname, float(growth), float(zrenie)))


    students.sort(key = lambda x: (x[2]))
    students.sort(key = lambda x: (x[3]))
    slice =  n_ryadov * n_desk
    out_list = []
    for i in range(0, s, slice):
        out_list.append(students[i:i + slice])
print(*out_list, sep = '\n')

The file contains the name, surname, height and vision of the students, which is described in this line:
name , surname , growth, zrenie = i
In general, the answer from this code will be as follows:
PS: I entered that there will be two rows and one person per desk
[('rq', 'rq', 178.0, -8.0), ('ty', 'ty', 189.0, -4.0)]
[('rt', 'rt', 190.0, -3.0), ('my', 'my', 170.0, -0.3)]
[('js ', 'js', 179.0, 0.3), ('a ', 'a ', 156.0, 0.4)]
[('ra', 'ra', 156.0, 0.8), ('mm ', 'mm', 180.0, 1.0)]

We see that the first element is the first name, the second is the last name, the third is height, the fourth is vision
Let's get to the point:
I want to get only the first and last name in the answer, so that it looks like this:
rq rq ty ty
I don't care how it looks, let even like this: But it is important for me that there would be only a first and last name . Please help, thanks in advance
[('rq' 'rq') ('ty''ty')]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmshar, 2022-02-05
@dmshar

I read and read, but did not understand anything. And what about TKINTER, what about CSV?? And here a sheet of code?
All that I managed to understand from the supplement is what you from the list


Gotta get a list

If so, then this is elementary:
it=
print(it)
ot=[]
for el in it:
    ot.append([el[0][0:2],el[1][0:2]])
print(ot)

The result of the work is shown above.
If that's not what you meant, then clarify your question so that it can be understood.

T
This_is_MonoliT, 2022-02-05
@This_is_MonoliT

a = ['Alex', '123', 'Mason', '1234']
print(a[0,2])
Output:
Alex Mason

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question