简体   繁体   中英

How to iterate over a list if lists in Python?

I have this list:

my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]

And try to iterate over it in order to get the values and its' positions, like so:

date    1
country 1
date    2
country 1
date    2

It should be later stored in a pandas df, values can be different, there is no fix.

Originally it was a list of dictionaries:

my_original_list = [
    [{'name': 'ga:date'}],
    [{'name': 'ga:country'}, {'name': 'ga:date'}],
    [{'name': 'ga:country'}, {'name': 'ga:date'}]
]

# But I got the values out of it in a list:
my_list = [li['name'] for li in my_original_list]

# the result
my_list = [
    ['ga:date'], 
    ['ga:country', 'ga:date'],
    ['ga:country', 'ga:date']
]

Cracked my mind already how to get it, would appreciate any help

Use list comprehension with enumerate and flattening for list of tuples:

my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]

x = [(b, a) for i in my_list for (a, b) in enumerate(i, 1)]
print (x)
[('ga:date', 1), ('ga:country', 1), ('ga:date', 2), ('ga:country', 1), ('ga:date', 2)]

df = pd.DataFrame(x, columns = ['field','listIndex'])
print (df)
        field  listIndex
0     ga:date          1
1  ga:country          1
2     ga:date          2
3  ga:country          1
4     ga:date          2

Or if possible change position of columns:

x1 = [z for i in my_list for z in enumerate(i, 1)]
print (x1)
[(1, 'ga:date'), (1, 'ga:country'), (2, 'ga:date'), (1, 'ga:country'), (2, 'ga:date')]

df = pd.DataFrame(x1, columns = ['listIndex','field'])
print (df)
   listIndex       field
0          1     ga:date
1          1  ga:country
2          2     ga:date
3          1  ga:country
4          2     ga:date

Also if need remove values before : :

my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]

x = [(b.split(':')[-1], a) for i in my_list for (a, b) in enumerate(i, 1)]
print (x)
[('date', 1), ('country', 1), ('date', 2), ('country', 1), ('date', 2)]

df = pd.DataFrame(x, columns = ['field','listIndex'])
print (df)
     field  listIndex
0     date          1
1  country          1
2     date          2
3  country          1
4     date          2

You can use enumerate for this:

my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]

for sublist in my_list:
    for position, entry in enumerate(sublist):
        print(entry, position + 1)  # +1 to count positions starting at 1 instead of 0.

How about this?

import pandas

my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]
df = pandas.DataFrame(data=[(sublist[i],i) for sublist in my_list for i in range(len(sublist))], columns=["field", "listIndex"])

REsult:

        field  listIndex
0     ga:date          0
1  ga:country          0
2     ga:date          1
3  ga:country          0
4     ga:date          1

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