我有一个字典,其中包含列表为值:
{
'List1' : ['Value1', 'Value2', 'Value3'],
'List2' : ['Value1', 'Value2', 'Value3'],
'List3' : ['Value1', 'Value2', 'Value3'],
}
我想迭代每个列表的值以查找正则言论,然后创建一个包含这些正则的字典。也就是说,对于我的初始字典列表。我列表上的每次迭代(在前一个示例中为3)都会创建1行(总共3行),因此我然后运行一个代码来制作一个全面的独特行。
不确定这是否清楚,但看起来应该与此相似:
for list in dictionary:
for value in list:
column_list_A = []
if re.search(regex, value):
column_list_A.append(regex, value).group(1)
column_list_B = []
if re.search(regex, value):
column_list_B.append(regex, value).group(1)
New_Dictionary = {"column_list_A" : column_list_A, "column_list_B" : column_list_B}
Df = pd.DataFrame.from_dict(New_Dictionary)
for column in Df:
#Code that puts the values of the 3 rows into 1 row
输出应该看起来像这样:
| Column_list_A | Column_list_B
----------------------------------------------------
List1 | match object | match object
----------------------------------------------------
List2 | match object | match object
----------------------------------------------------
List3 | match object | match object
我的问题是:
1)如何实现嵌套循环?我已经尝试使用IterItems()之类的东西,但没有给出令人满意的结果。每个循环的x和y到底应该是什么?
2)凹痕正确吗?
如果您希望最终输出成为数据框架,我建议您使用熊猫功能,这些功能可以自己处理循环和正则频率,而无需循环。这是一个例子:
import pandas as pd
# read dict in the right orientation
df = pd.DataFrame.from_dict(dictionary, orient="index")
''' # your df will look like this:
>>> df
0 1 2
List1 Value1 Value2 Value3
List2 Value1 Value2 Value3
List3 Value1 Value2 Value3
'''
# append your regex matches to the dataframe
# e.g. match any of (d,e) followed by a digit
df["match_from_column_0"] = df[0].str.extract(r'([de]d)')
# e.g. match a digit
df["match_from_column_1"] = df[1].str.extract(r'(d)')
# save your output as a dataframe
output = df[["match_from_column_0","match_from_column_1"]]
''' # output will look like this:
>>> output
match_from_column_0 match_from_column_1
List1 e1 2
List2 e1 2
List3 e1 2
'''
# or a dict
output_dict = output.to_dict()
'''
>>> output_dict
{'output1': {'List1': 'e1', 'List2': 'e1', 'List3': 'e1'},
'output2': {'List1': 'e2', 'List2': 'e2', 'List3': 'e2'}}
'''
解决您的两个问题:
- 词典上的循环可能是类似的(假设python3):
for dict_key, dict_value in dictionary.items(): # do whatever
- 列表的循环可能是:
for value in my_list: # do whatever
您的行3-8应该被灭绝(从第二个空格开始循环凹痕)
要以自己的方式执行此操作(我认为更难的方式)是一个建议(if语句应该需要一个其他条款 附加空字符串,因为它们会导致您的列表长度不平等吗?):
import re
for key, list_of_values in dictionary.items():
for value in list_of_values:
column_list_A = []
if re.search(regex, value):
column_list_A.append(re.search(regex, value).group(0))
else:
column_list_A.append("")
column_list_B = []
if re.search(regex, value):
column_list_B.append(re.search(regex, value).group(0))
else:
column_list_B.append("")
New_Dictionary = {"column_list_A" : column_list_A, "column_list_B" : column_list_B}
Df = pd.DataFrame.from_dict(New_Dictionary)
for column in Df:
# do your thing
对文档的一些引用:
- Panda Regex提取
- pandas dataframe from_dict
希望有帮助!
如果您可以使用以下DICTOMP:
import re
from pprint import pprint
d = {
'List1' : ['Value1', 'Value2', 'Value3'],
'List2' : ['Value1', 'Value2', 'Value3'],
'List3' : ['Value1', 'Value2', 'Value3'],
}
col = ["column_list_A", "column_list_B", "column_list_C"]
def func(a, b, c):
a = re.match(r'Val(ued)', a).group(1)
b = re.match(r'Valu(ed)', b).group(1)
c = re.match(r'Value(d)', c).group(1)
return [a, b, c]
new_d = {i: func(*j) for i, *j in zip(col, *d.values())}
pprint(new_d)
输出:
{'column_list_A': ['ue1', 'e1', '1'],
'column_list_B': ['ue2', 'e2', '2'],
'column_list_C': ['ue3', 'e3', '3']}