Dan_99
Dan_99

Reputation: 49

For enumerate Iteration

for v in enumerate (["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"])
    folder = "/Users/Name/Desktop/Datas/" + v
    csvFiles = glob.glob (os.path.join (folder, "*.csv"))
    df = (pd.read_csv(i) for i in csvFiles)
    df = pd.concat (df, ignore_index=True)

I´m iterating over the subfolders A,B,C,D,E,F,G,H,I,J,K.

This code works good, but how can I avoid writing all the string ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"] in the for sentence?

Upvotes: 2

Views: 61

Answers (3)

cgte
cgte

Reputation: 450

Strings are iterables too :)

    Python 2.7.14+ (default, Dec  5 2017, 15:17:02) 
    [GCC 7.2.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> for x in enumerate('ABC'):
    ...     print x
    ... 
    (0, 'A')
    (1, 'B')
    (2, 'C')

And you can compare strings :

    >>> 'A' < 'V'
    True
    >>> 'E' < 'V'
    True
    >>> 'E' < 'D'
    False
    >>> 'A' < 'C' < 'K'
    True

Upvotes: 0

steliosbl
steliosbl

Reputation: 8921

If you're working based on the number, you can do like so:

import string
letters = [string.ascii_uppercase[i] for i in range(11)]

Upvotes: 1

Gerges
Gerges

Reputation: 6519

If you are using consecutive alphabets, you can use:

for v in range(ord('A'), ord('K')+1):
    print(chr(v))

Upvotes: 0

Related Questions