简体   繁体   中英

Assign glob.iglob() results to a list

The glob.iglob() -function returns an iterator. How can I most effectively assign its "items" to a list in Python? I want the results in a list so that I can choose a random item with random.choice() .

You could use glob.glob instead since it returns a list.

To build a list out of an arbitrary iterable, you just use the list builtin:

list(glob.iglob('*'))

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