简体   繁体   English

迭代 python 中的二维列表并打印每个元素的标题

[英]iteration over 2d list in python and printing headings for each element

I am trying to work out how to iterate over a list and print out each item with a print statement describing what element is.我正在尝试解决如何遍历列表并使用描述元素是什么的打印语句打印出每个项目。 my project is to create a user management system and print out something similar to the image I have attached.我的项目是创建一个用户管理系统并打印出类似于我所附图像的内容。

The output I am trying to produce我正在尝试生产的 output

The output I am getting我得到的 output

My code:我的代码:

records = 0
userFirst = ["John"]
userLast = ["Doe"]
autoUsername = ["Johndoe91"]
autoPassword = ["123456789"]
hiddenPassword = ["*****789"]
userRole = ["User"]
userDept = ["Administration"]
users = []  
confidentialUserDetails = []  

users.append(userFirst + userLast + userRole + userDept + autoUsername + autoPassword)
confidentialUserDetails.append(users)

for row in range(len(confidentialUserDetails)):
    records += 1
    print("-" * 25)
    print("Record: ", records)
    for col in range(len(confidentialUserDetails[row])):
        print(confidentialUserDetails[row][col])

Any help would be greatly appreciated.任何帮助将不胜感激。 :) :)

I created a dictionary called user instead of your list and after that I appended it to the second list and finally I printed the key and the value of the dictionary.我创建了一个名为user的字典而不是你的列表,然后我将它附加到第二个列表中,最后我打印了字典的键和值。

Also to get the full name I joined userFirst and userLast as string.为了获得全名,我将userFirstuserLast作为字符串加入。

Code:代码:

records = 0
userFirst = ["John"]
userLast = ["Doe"]
autoUsername = ["Johndoe91"]
autoPassword = ["123456789"]
hiddenPassword = ["*****789"]
userRole = ["User"]
userDept = ["Administration"]

confidentialUserDetails = []  # 2d list for asterisked passwords

users={'Name' : [' '.join(userFirst + userLast)] ,'Role' : userRole , 'Departement' : userDept ,'Username' : autoUsername ,'Password' : hiddenPassword }
confidentialUserDetails.append(users)

for user in confidentialUserDetails:
    records += 1
    print("-" * 25)
    print("Record: ", records)
    for ele,v in user.items():
        print(ele,':',v[0])

Output: Output:

-------------------------
Record:  1
Name : John Doe
Role : User
Departement : Administration
Username : Johndoe91
Password : *****789

Your data structures are unusual.您的数据结构不寻常。 I'm assuming that those lists are going to be provided to your code somehow and will, in practice, have multiple user details appended to them so that they are all the same length.我假设这些列表将以某种方式提供给您的代码,并且在实践中会附加多个用户详细信息,以便它们的长度相同。

Anyhow, you can achieve the output you're looking for with some readable f-strings like this:无论如何,您可以使用如下一些可读的 f 字符串来实现您正在寻找的 output:

from functools import reduce

userFirst = ["John"]
userLast = ["Doe"]
autoUsername = ["Johndoe91"]
autoPassword = ["123456789"]
hiddenPassword = ["*****789"]
userRole = ["User"]
userDept = ["Administration"]

for row in range(len(userFirst)):
    s = (f"""\
Name          : {userFirst[row]} {userLast[row]}
Role          : {userRole[row]}
Department    : {userDept[row]}
Username      : {autoUsername[row]}
Password      : {hiddenPassword[row]}""")

    maxlen = reduce(lambda x,y: max(x, len(y)), s.split("\n"), 0)
    print(f"{s}\n{'-'*maxlen}\n")

Output: Output:

Name          : John Doe
Role          : User
Department    : Administration
Username      : Johndoe91
Password      : *****789
------------------------------

Using a dictionary or f strings like the two other answers suggested is probably the best.使用像建议的其他两个答案那样的字典或 f 字符串可能是最好的。 But if you just want to use your current code to print your desired output, you can simply grab each item by its index number in your print statement.但是,如果您只想使用当前代码打印所需的 output,您只需在打印语句中按其索引号获取每个项目。

Change the line:换行:

print(confidentialUserDetails[row][col])

To something like this:对于这样的事情:

print("Name          : ", confidentialUserDetails[row][col][0], confidentialUserDetails[row][col][1])
print("Role:         : ", confidentialUserDetails[row][col][2])

Output: Output:

-------------------------
Record:  1
Name          :  John Doe
Role:         :  User

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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