Если вы хотите, чтобы ваш конечный вывод был фреймом данных, я бы посоветовал вам использовать функции panda, которые могут самостоятельно обрабатывать циклы и выполнять регулярные выражения без необходимости в циклах. Вот пример:
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'}}
'''
Чтобы ответить на ваши 2 вопроса:
for dict_key, dict_value in dictionary.items():
# do whatever
for value in my_list:
# do whatever
Ваши строки 3-8 должны быть устаревшими (4 пробела от вашего второго для отступа цикла)
[ 119]
Чтобы сделать это по-своему (на мой взгляд, более сложным способом), вот предложение (операторам if нужно использовать условие else + добавить пустую строку, так как они приведут к тому, что ваши списки будут иметь неравную длину?):
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
Некоторые ссылки на документацию:
Надеюсь, это поможет!
Что не так с нулем? В случае отсутствия int
против null
, попробуйте int?
:
exp = Expression.Equal(
Expression.Constant(10, typeof(int?)),
Expression.Constant(null, typeof(int?))
);