简体   繁体   中英

Used for loop to print the row count of the array then data duplicates base on length of my array, how do I print only 1 line of data in Python?

How do I print only 1 copy of data? I am a complete beginner. This is Python code I did for Add data.

This is my current output:

Row | Name | Age | Grade
1     jo     23     1
2     jo     23     1
1     ki     24     2
2     ki     24     2

This is my expected output:

Row | Name | Age | Grade
1     jo     23     1
2     ki     24     2

Here is my code:

name = []
age = []
grade = []
record = []
rowCount = 0
choice = 1
while int(choice) != 0:
    if int(choice) > 4 or int(choice) < 1:
        choice = input ("Invalid input. Enter choice: ")
    else:
        print("Main Menu:")
        print("1- Add")
        print("2- Delete")
        print("3- Edit")
        print("4- Print Report")
        print("0- End Program")
        choice = input ("Enter choice: ")
        if int(choice) == 1: #Add function
            name.append(input("Input Name: "))
            age.append(input("Input Age: "))
            grade.append(input("Input Grade: "))
            print ("Row | Name | Age | Grade")
            for x, y, z in zip(name,age,grade):
                for w in range(len(name)):
                    print(str(w+1)  + "     " + x + "     " + y + "     " + z)
                    w+1
        elif int(choice) == 2: #Delete function
            print("Delete func")
        elif int(choice) == 3: #Update function
            print("Edit func")
        elif int(choice) == 4: #Print function
            print("Print func")
print("Ending Program...")

For each entry in zip(name, age, grade) , you have another nested loop (for w ). This causes the program to print multiple rows per entry. One way to solve it is to include w inside the zip ; ie, replacing

for x, y, z in zip(name,age,grade):
    for w in range(len(name)):
        print(str(w+1)  + "     " + x + "     " + y + "     " + z)
        w+1

with

for x, y, z, w in zip(name, age, grade, range(len(name))):
    print(str(w+1)  + "     " + x + "     " + y + "     " + z)

You could use a list of lists for each group of data, then use set function

For example:

students = []
...
while True:
   name = input("Input Name: ")
   age = input("Input Age: ")
   grade = input("Input Grade: ")
   student.append([name, age, grade])
   

Then, to get only the unique data on your list of lists, you can use set, but set doesn't work on list of lists, so you can iterate though it and treat each list as a tuple, then use set on the tuples, which I adapted from this answer ie:

unique_students = set(tuple(row) for row in (students)))

Now you have a new variable with the unique data from students and you can iterate though it and print the data as you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM