简体   繁体   中英

How Do I Parse A Python Tuple To Print Each Value On New Line?

Assuming I have a Python dictionary like so:

GroupName [('G:', 'G:/path/to/folder1', 'Full Access'), ('G:', 'G:/path/to/folder2', 'Full Access'), ('G:', 'G:/path/to/folder3', 'Full Access')]

How do I loop through it to print each entry on a new line like so:

GroupName G:/path/to/folder1 Full Access
GroupName G:/path/to/folder2 Full Access
GroupName G:/path/to/folder3 Full Access

I know that you can do

for k,v in GroupName.items:
    print k,v

which results in

GroupName [('G:', 'G:/path/to/folder1', 'Full Access'), ('G:', 'G:/path/to/folder2', 'Full Access'), ('G:', 'G:/path/to/folder3', 'Full Access')]

But I am unsure how to further parse v to print the values individually.

Assistance would be appreciated.

Thank You!

You are trying to print a list of tuples. You probably want to do something like

for e in GroupName:
    print("GroupName " + e[1] + " " + e[2])

Your problem is that you aren't dealing with a dictionary, there are no key value pairs here. What you have is a list of tuples: there are two different data types present. You will need to iterate through the list of groups, and deal with the values of each group tuple.

This produces your desired result:

group_name = [('G:', 'G:/path/to/folder1', 'Full Access'), ('G:', 'G:/path/to/folder2', 'Full Access'), ('G:', 'G:/path/to/folder3', 'Full Access')]

for group in group_name:
    print(*(group_property for group_property in group))

This loops through the list with for group in group_name and unpacks and prints the tuple that makes up each element in the list, using print(*(group_property for group_property in group))

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