简体   繁体   中英

Python, print value and List in one line

I know that print itemSets (where itemSets is a List) will automatically print the contents of the List in Python. I have a List of a class where each instance of the class has a List that I want to print along with another value in one line. For example, if my code was currently:

for i, d in enumerate(database):
    print d.itemSets

Is there a built in way to print i (or another value) along with the List ( itemSets )? Instead of the output:

[{'48': 0}]
[{'46': 0}, {'40': 0}]
[{'40': 0}]

I would want something like:

0 -> [{'48': 0}]
1 -> [{'46': 0}, {'40': 0}]
2 -> [{'40': 0}]

The -> is a meaningless string, it is just there so that I can read the output easily

You'd just include the index, which you've already saved to i :

for i, d in enumerate(database):
    print i, '->', d.itemSets

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