如何从 python 中的巨大列表列中拆分所有其他元素
How to split every other element from a huge column of list in python
我在 python 的单列中有一个巨大的列表,我需要从列表中拆分所有水果、颜色等并制作一个 dafaframe。
example
details=['banana',
'type:',
'fruit',
'color:',
'yellow',
'orange',
'type:',
'fruit',
'color:',
'orange',
'blueberry',
'type:',
'fruit',
'color:',
'blue']
我期望实现的是,如果我从上面提取所有颜色,那么结果应该是如下所示的单列列表。
Out[1]:
['yellow',
'orange',
'blue']
一种可能的方法如果数据结构没有改变,是使用列表理解:
例如:[details[i+1] for i, x in enumerate(details) if x == 'color:']
完整代码:
details=['banana',
'type:',
'fruit',
'color:',
'yellow',
'orange',
'type:',
'fruit',
'color:',
'orange',
'blueberry',
'type:',
'fruit',
'color:',
'blue']
colors = [details[i+1] for i, x in enumerate(details) if x == 'color:']
fruits = [details[i-1] for i, x in enumerate(details) if x == 'type:']
types = [details[i+1] for i, x in enumerate(details) if x == 'type:']
print('fruits: ', fruits)
print('types: ', types)
print('colors: ', colors)
输出:
fruits: ['banana', 'orange', 'blueberry']
types: ['fruit', 'fruit', 'fruit']
colors: ['yellow', 'orange', 'blue']
或作为数据框
# to make datafame
import pandas as pd
df = pd.DataFrame()
df['fruits'] = [details[i-1] for i, x in enumerate(details) if x == 'type:']
df['types'] = [details[i+1] for i, x in enumerate(details) if x == 'type:']
df['colors'] = [details[i+1] for i, x in enumerate(details) if x == 'color:']
print(df)
输出:
fruits types colors
0 banana fruit yellow
1 orange fruit orange
2 blueberry fruit blue
解释:
- 颜色值在字符串
color
之后,因此在列表 中找到该字符串的索引
- 在这个索引中添加1得到color
的值
- 将其与列表理解结合使用以获得所需的输出数组
- 重复其他值
details=['banana',
'type:',
'fruit',
'color:',
'yellow',
'orange',
'type:',
'fruit',
'color:',
'orange',
'blueberry',
'type:',
'fruit',
'color:',
'blue']
# split each fruit as a list and index colors
details = [details[i:i+5] for i in range(0,len(details),5)]
fruits = []
color = []
for i in details:
fruits.append(i[0])
color.append(i[4])
如果只想提取颜色,可以使用包 colour
- https://github.com/vaab/colour
In [7]: from colour import Color
In [8]: details
Out[8]:
['banana',
'type:',
'fruit',
'color:',
'yellow',
'orange',
'type:',
'fruit',
'color:',
'orange',
'blueberry',
'type:',
'fruit',
'color:',
'blue']
In [9]: colors_only = set()
In [10]: for i in details:
...: try:
...: Color(i)
...: colors_only.add(i)
...: except: pass
...:
In [11]: colors_only
Out[11]: {'yellow', 'orange', 'blue'}
我在 python 的单列中有一个巨大的列表,我需要从列表中拆分所有水果、颜色等并制作一个 dafaframe。
example
details=['banana',
'type:',
'fruit',
'color:',
'yellow',
'orange',
'type:',
'fruit',
'color:',
'orange',
'blueberry',
'type:',
'fruit',
'color:',
'blue']
我期望实现的是,如果我从上面提取所有颜色,那么结果应该是如下所示的单列列表。
Out[1]:
['yellow',
'orange',
'blue']
一种可能的方法如果数据结构没有改变,是使用列表理解:
例如:[details[i+1] for i, x in enumerate(details) if x == 'color:']
完整代码:
details=['banana',
'type:',
'fruit',
'color:',
'yellow',
'orange',
'type:',
'fruit',
'color:',
'orange',
'blueberry',
'type:',
'fruit',
'color:',
'blue']
colors = [details[i+1] for i, x in enumerate(details) if x == 'color:']
fruits = [details[i-1] for i, x in enumerate(details) if x == 'type:']
types = [details[i+1] for i, x in enumerate(details) if x == 'type:']
print('fruits: ', fruits)
print('types: ', types)
print('colors: ', colors)
输出:
fruits: ['banana', 'orange', 'blueberry']
types: ['fruit', 'fruit', 'fruit']
colors: ['yellow', 'orange', 'blue']
或作为数据框
# to make datafame
import pandas as pd
df = pd.DataFrame()
df['fruits'] = [details[i-1] for i, x in enumerate(details) if x == 'type:']
df['types'] = [details[i+1] for i, x in enumerate(details) if x == 'type:']
df['colors'] = [details[i+1] for i, x in enumerate(details) if x == 'color:']
print(df)
输出:
fruits types colors
0 banana fruit yellow
1 orange fruit orange
2 blueberry fruit blue
解释:
- 颜色值在字符串
color
之后,因此在列表 中找到该字符串的索引
- 在这个索引中添加1得到color 的值
- 将其与列表理解结合使用以获得所需的输出数组
- 重复其他值
details=['banana',
'type:',
'fruit',
'color:',
'yellow',
'orange',
'type:',
'fruit',
'color:',
'orange',
'blueberry',
'type:',
'fruit',
'color:',
'blue']
# split each fruit as a list and index colors
details = [details[i:i+5] for i in range(0,len(details),5)]
fruits = []
color = []
for i in details:
fruits.append(i[0])
color.append(i[4])
如果只想提取颜色,可以使用包 colour
- https://github.com/vaab/colour
In [7]: from colour import Color
In [8]: details
Out[8]:
['banana',
'type:',
'fruit',
'color:',
'yellow',
'orange',
'type:',
'fruit',
'color:',
'orange',
'blueberry',
'type:',
'fruit',
'color:',
'blue']
In [9]: colors_only = set()
In [10]: for i in details:
...: try:
...: Color(i)
...: colors_only.add(i)
...: except: pass
...:
In [11]: colors_only
Out[11]: {'yellow', 'orange', 'blue'}