Python 从具有偶然模式的列表中提取元素到元组中
Python extract elements from list with occasional pattern into a tuple
我有一个列表,它有这样的模式 [float, string, float string...] 但偶尔在模式中它会更改为 [float, string, float, string, string, float string.. .]。我想要做的是将列表的元素提取到格式为 (float, name, NoneType or str) 的元组中,以便稍后对其进行处理。这是一个小例子:
arr = [1150.1, 'James', 3323.1, 'Steve', 9323.1, 'John', 1233.1, 'Gary', 'criminal', 3293.1, 'Josh', 9232.1, 'Daniel', 'criminal']
我想提取列表,使元组看起来像这样:
(1150.1, James, NONE)
(3323.1, Steve, NONE)
(9323.1, John, NONE)
(1233.1, Gary, criminal)
(3293.1, Josh, NONE)
(9232.1, Daniel, criminal)
到目前为止,我已经尝试检查该类型数组中的下一个索引,但它不起作用:
for index in range(len(arr)):
if type(arr[index]) == float and type(arr[index+1]) == str:
tup = arr[index], arr[index+1], None
print(tup)
elif type(arr[index]) == float and type(arr[index+1]) == str and type(arr[index+2]) == str:
tup = arr[index], arr[index + 1], arr[index+2]
print(tup)
您可以使用辅助列表跟踪自上次浮动值以来看到的数组元素。每当看到float,就把已有的元素转成元组,清空辅助列表:
result = []
items = []
for item in arr:
if isinstance(item, float) and items:
if len(items) < 3:
items.append(None)
result.append(tuple(items))
items = [item]
else:
items.append(item)
result.append(tuple(items))
print(result)
这输出:
[
(1150.1, 'James', None), (3323.1, 'Steve', None),
(9323.1, 'John', None), (1233.1, 'Gary', 'criminal'),
(3293.1, 'Josh', None), (9232.1, 'Daniel', 'criminal')
]
您可以检查“浮动”、“字符串”模式并相应地附加:
output = list()
for i, element in enumerate(arr):
if isinstance(element, float) and isinstance(arr[i+1], str):
if isinstance(arr[i+2], str):
t = tuple(arr[i:i+3])
else:
t = tuple(arr[i:i+2]+["NONE"])
output.append(t)
>>> output
[(1150.1, 'James', 'NONE'),
(3323.1, 'Steve', 'NONE'),
(9323.1, 'John', 'NONE'),
(1233.1, 'Gary', 'criminal'),
(3293.1, 'Josh', 'NONE'),
(9232.1, 'Daniel', 'criminal')]
另一个解决方案:
from itertools import groupby
g1 = (g for v, g in groupby(arr, type) if v is float)
g2 = (g for v, g in groupby(arr, type) if v is str)
out = [(next(a), *[*b, None][:2]) for a, b in zip(g1, g2)]
print(out)
打印:
[
(1150.1, "James", None),
(3323.1, "Steve", None),
(9323.1, "John", None),
(1233.1, "Gary", "criminal"),
(3293.1, "Josh", None),
(9232.1, "Daniel", "criminal"),
]
我有一个列表,它有这样的模式 [float, string, float string...] 但偶尔在模式中它会更改为 [float, string, float, string, string, float string.. .]。我想要做的是将列表的元素提取到格式为 (float, name, NoneType or str) 的元组中,以便稍后对其进行处理。这是一个小例子:
arr = [1150.1, 'James', 3323.1, 'Steve', 9323.1, 'John', 1233.1, 'Gary', 'criminal', 3293.1, 'Josh', 9232.1, 'Daniel', 'criminal']
我想提取列表,使元组看起来像这样:
(1150.1, James, NONE)
(3323.1, Steve, NONE)
(9323.1, John, NONE)
(1233.1, Gary, criminal)
(3293.1, Josh, NONE)
(9232.1, Daniel, criminal)
到目前为止,我已经尝试检查该类型数组中的下一个索引,但它不起作用:
for index in range(len(arr)):
if type(arr[index]) == float and type(arr[index+1]) == str:
tup = arr[index], arr[index+1], None
print(tup)
elif type(arr[index]) == float and type(arr[index+1]) == str and type(arr[index+2]) == str:
tup = arr[index], arr[index + 1], arr[index+2]
print(tup)
您可以使用辅助列表跟踪自上次浮动值以来看到的数组元素。每当看到float,就把已有的元素转成元组,清空辅助列表:
result = []
items = []
for item in arr:
if isinstance(item, float) and items:
if len(items) < 3:
items.append(None)
result.append(tuple(items))
items = [item]
else:
items.append(item)
result.append(tuple(items))
print(result)
这输出:
[
(1150.1, 'James', None), (3323.1, 'Steve', None),
(9323.1, 'John', None), (1233.1, 'Gary', 'criminal'),
(3293.1, 'Josh', None), (9232.1, 'Daniel', 'criminal')
]
您可以检查“浮动”、“字符串”模式并相应地附加:
output = list()
for i, element in enumerate(arr):
if isinstance(element, float) and isinstance(arr[i+1], str):
if isinstance(arr[i+2], str):
t = tuple(arr[i:i+3])
else:
t = tuple(arr[i:i+2]+["NONE"])
output.append(t)
>>> output
[(1150.1, 'James', 'NONE'),
(3323.1, 'Steve', 'NONE'),
(9323.1, 'John', 'NONE'),
(1233.1, 'Gary', 'criminal'),
(3293.1, 'Josh', 'NONE'),
(9232.1, 'Daniel', 'criminal')]
另一个解决方案:
from itertools import groupby
g1 = (g for v, g in groupby(arr, type) if v is float)
g2 = (g for v, g in groupby(arr, type) if v is str)
out = [(next(a), *[*b, None][:2]) for a, b in zip(g1, g2)]
print(out)
打印:
[
(1150.1, "James", None),
(3323.1, "Steve", None),
(9323.1, "John", None),
(1233.1, "Gary", "criminal"),
(3293.1, "Josh", None),
(9232.1, "Daniel", "criminal"),
]