python 如何将不同列表中的每个元素连接在一起?

amrnrhlw  于 2023-01-08  发布在  Python
关注(0)|答案(3)|浏览(131)
print(sgrades_flat)   
['Barrett', 'Edan', '70', '45', '59', 'Bradshaw', 'Reagan', '96', '97', '88', 'Charlton', 'Caius', '73', '94', '80', 'Mayo', 'Tyrese', '88', '61', '36', 'Stern', 'Brenda', '90', '86', '45']

print(s_grades)   
['F', 'A', 'B', 'D', 'C']

我想将sgrades_flat和s_grades组合起来,看起来像...

['Barrett', 'Edan', '70', '45', '59', 'F',   
'Bradshaw', 'Reagan', '96', '97', '88', 'A'  
'Charlton', 'Caius', '73', '94', '80', 'B'   
'Mayo', 'Tyrese', '88', '61', '36', 'D'   
'Stern', 'Brenda', '90', '86', '45', 'C']

我目前的策略是使用以下代码:

z=[]    
for i, x in zip(sgrades_flat[::5], s_grades):
  z.append(i+x)
print(z)

但输出是:

['BarrettF', 'BradshawA', 'CharltonB', 'MayoD', 'SternC']
fsi0uk1n

fsi0uk1n1#

我将通过手动迭代来合并列表:

sgrades_flat=['Barrett', 'Edan', '70', '45', '59', 'Bradshaw', 'Reagan', '96', '97', '88', 'Charlton', 'Caius', '73', '94', '80', 'Mayo', 'Tyrese', '88', '61', '36', 'Stern', 'Brenda', '90', '86', '45']

s_grades=['F', 'A', 'B', 'D', 'C']

it1 = iter(sgrades_flat)
it2 = iter(s_grades)

result = []
try:
    while True:
        for _ in range(5):
            result.append(next(it1))
        result.append(next(it2))

except StopIteration:
    pass

print(result)

印刷品

['Barrett', 'Edan', '70', '45', '59', 'F', 
 'Bradshaw', 'Reagan', '96', '97', '88', 'A',
 'Charlton', 'Caius', '73', '94', '80', 'B',
 'Mayo', 'Tyrese', '88', '61', '36', 'D', 
 'Stern', 'Brenda', '90', '86', '45', 'C']

(this仍然看起来像是一个坏主意,因为平面列表对于这样的数据结构是次优的)
请注意,没有任何手动迭代的一行程序也执行相同的操作:

import itertools
grouped_sgrades = list(itertools.chain.from_iterable(
       sgrades_flat[i:i+5]+[s_grades[i//5]] 
       for i in range(0,len(sgrades_flat),5)))

然而,为什么要把清单扁平化呢?

grouped_sgrades = [sgrades_flat[i:i+5]+[s_grades[i//5]] for i in range(0,len(sgrades_flat),5)]

result是一个很好的列表列表,它接近于某种结构化数据:

[['Barrett', 'Edan', '70', '45', '59', 'F'],
 ['Bradshaw', 'Reagan', '96', '97', '88', 'A'],
 ['Charlton', 'Caius', '73', '94', '80', 'B'],
 ['Mayo', 'Tyrese', '88', '61', '36', 'D'],
 ['Stern', 'Brenda', '90', '86', '45', 'C']]
ssgvzors

ssgvzors2#

您可以使用enumerate,然后相应地对sgrades_flat进行切片:

sgrades_flat = [
    'Barrett', 'Edan', '70', '45', '59',
    'Bradshaw', 'Reagan', '96', '97', '88',
    'Charlton', 'Caius', '73', '94', '80',
    'Mayo', 'Tyrese', '88', '61', '36',
    'Stern', 'Brenda', '90', '86', '45']

s_grades = ['F', 'A', 'B', 'D', 'C']

z = []
for i, x in enumerate(s_grades):
    z += sgrades_flat[5*i:5*(i+1)] + [x]

print(z)
oymdgrw7

oymdgrw73#

我们需要同时迭代s_gradessgrades_flat,每次需要迭代5个元素。我们可以使用基于sgrades_flat长度1/5的x1m2 n1 a列表解析。然后,我们可以使用列表解析中的第二级迭代将j绑定到i * 5索引。

[
 [*sgrades_flat[j:j+5], s_grades[i]] 
 for i in range(len(sgrades_flat) // 5)
 for j in (i * 5,)
]
# [['Barrett',  'Edan',   '70', '45', '59', 'F'], 
#  ['Bradshaw', 'Reagan', '96', '97', '88', 'A'], 
#  ['Charlton', 'Caius',  '73', '94', '80', 'B'], 
#  ['Mayo',     'Tyrese', '88', '61', '36', 'D'], 
#  ['Stern',    'Brenda', '90', '86', '45', 'C']]

或者,使用enumerates_grades上迭代:

[
 [*sgrades_flat[j:j+5], g] 
 for i, g in enumerate(s_grades)
 for j in (i * 5,)
]

# [['Barrett',  'Edan',   '70', '45', '59', 'F'], 
#  ['Bradshaw', 'Reagan', '96', '97', '88', 'A'], 
#  ['Charlton', 'Caius',  '73', '94', '80', 'B'], 
#  ['Mayo',     'Tyrese', '88', '61', '36', 'D'], 
#  ['Stern',    'Brenda', '90', '86', '45', 'C']]

相关问题