python - 将元素添加到python pyspark中的列表列表

标签 python list pyspark

我有一个包含学生数据结构的列表列表 - [('100', 'class 1'), ('110', 'class 2'), ('1030', 'class 2' ),.....]。我使用这个命令得到的这个结构:

student_class = student.map(lambda x:x).zip(class)

现在我想将学生分数添加到上面列表列表的每个元素中,这样输出就变成了:

[('100', 'class 1',score1), ('110', 'class 2',score2), ('1030', 'class 2',score3),.... .]

学生的分数显示在名为score 的列表中。如何将分数添加到上面的 student_class 列表中,使其成为 student_classScore 列表?

最佳答案

因为你有一个 listtuple 并且 tuple 是不可变的,你需要通过集中现有的元组来创建一个新的元组元组与单元素元组(由 (score,) 创建),然后将新创建的元组附加到新列表。

l = [('100', 'class 1'), ('110', 'class 2'), ('1030', 'class 2')]
scores = [1, 2, 3]
new_list = []
for tup, score in zip(l, scores):
    new_list.append(tup + (score,))

print new_list
>> [('100', 'class 1', 1), ('110', 'class 2', 2), ('1030', 'class 2', 3)]

另一种但不那么pythonic但概念相同的方式:

for index, tup in enumerate(l):
    l[index] = tup + (scores[index],)
print l
>> [('100', 'class 1', 1), ('110', 'class 2', 2), ('1030', 'class 2', 3)]

关于python - 将元素添加到python pyspark中的列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32013244/

相关文章:

python - 无法使 Python 列表副本起作用

python - 获取列表部分的相应总和

python - 将多列从行旋转到列

amazon-s3 - Pyspark 不使用 TemporaryAWSCredentialsProvider

python - python中的列表列表集

python - 根据时间间隔连接 Pandas 数据帧并计算平均值

python - 使用分隔符列出的列

string - 如何在 Scala 中迭代列表的每个元素时修改它?

apache-spark - Spark : return null from failed regexp_extract()

python - 使用 argparse 创建相互包含的位置参数