[英]Python: Saving all instances from inside a loop to variable to call later
I'm working with Pandas and a CSV file that I'm iterating through in order to find every occurrence of a specific color and then returning the image's name. 我正在处理熊猫和要遍历的CSV文件,以便查找每种特定颜色的情况,然后返回图像的名称。 For example, I have a dataframe:
例如,我有一个数据框:
imageName color1 color2 color3 color4
1 Blue Blue Blue Blue
2 Pink Magenta Red Purple
3 Blue Blue Blue Blue
4 Blue Yellow Teal Teal
5 Yellow Blue Red Purple
...
What I'm doing is printing the image name each time a specific color is mentioned. 我正在做的是每次提到特定颜色时都打印图像名称。
for index, row in df.iterrows():
if (row['color1'] == 'Blue' and row['color2'] == 'Blue' and row['color3'] == 'Blue' and row['color4'] == 'Blue'):
allBlue = (row['imageName'])
This gets me the results that I want, but if I call my variable allBlue from a different file, it just returns the last instance since it's not in a for loop. 这样可以得到所需的结果,但是如果我从另一个文件中调用变量allBlue,则它仅返回最后一个实例,因为它不在for循环中。 Is there a way to save the whole print out to a variable to call later from a different file?
有没有一种方法可以将整个打印结果保存到一个变量中,以便以后从另一个文件中调用?
Like this: When I call it from inside loop 像这样:当我从内部循环调用它时
Occurrences of all Blue Shapes:
1
3
6
19
...
178
Instead of this: When I call it from other file 而不是这样:当我从其他文件调用它时
Occurrences of all Blue Shapes:
178
I'm fairly certain I'm just forgetting something obvious or not thinking about something clearly, but any help would be much appreciated. 我可以肯定的是,我只是忘记了一些显而易见的事情,或者没有在清晰地思考某些事情,但是任何帮助将不胜感激。
You don't need the loop to find images-with-a-color in pandas. 您无需循环即可在熊猫中查找彩色图像。 However, I put in a loop that uses the accumulator-list in the right place as an example (
colorcols
): 但是,我放入了一个循环,该循环在正确的位置使用了accumulator-list作为示例(
colorcols
):
import pandas as pd
cdf = pd.DataFrame({'color1': {0: 'Red', 1: 'Blue', 2: 'Yellow', 3: 'Blue'},
'color2': {0: 'Red', 1: 'Green', 2: 'Blue', 3: 'Blue'},
'color3': {0: 'Red', 1: 'Red', 2: 'Red', 3: 'Blue'},
'color4': {0: 'Red', 1: 'Blue', 2: 'White', 3: 'Blue'},
'form': {0: 'circle', 1: 'circle', 2: ' square', 3: 'circle'},
'imageName': {0: 'img1', 1: 'img2', 2: 'img3', 3: 'img4'}})
colorcols = []
for i in range(1,5):
colorcols.append('color%d'%i)
for color in set(cdf[colorcols].values.flatten()):
mask = (cdf==color)
print('%s: '%color),
colors = (mask.sum(axis=1) > 0)
print(cdf[colors].imageName.values)
Blue: ['img2' 'img3' 'img4'] White: ['img3'] Green: ['img2'] Yellow: ['img3'] Red: ['img1' 'img2' 'img3']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.