gpt4 book ai didi

python - 如何将此Python逗号分隔的字符串列表分成多个键值对?

转载 作者:行者123 更新时间:2023-12-02 21:05:42 24 4
gpt4 key购买 nike

如果我有表格的输入
((0, 1), A 0 0.0), ((0, 2), A 0 0.0), ((0, 0), A 0 0.0),其中(0,1) , (0,2), (0,0)是键,如何将它们分成多个键值对。

例如,如果我要打印键,则上面的值应返回:

(0,1) , A 0 0 0.0
(0,2) , A 0 0 0.0
(0,0) , A 0 0 0.0

此输出将在我的Reducer函数中使用,其代码为:
import sys
import string
import numpy
import re


#number of columns of A/rows of B
n = int(sys.argv[1])

#Create data structures to hold the current row/column values (if needed; your code goes here)

currentkey = None
# input comes from STDIN (stream data that goes to the program)
for line in sys.stdin:
#print(line)
#Remove leading and trailing whitespace
#line = line.strip().replace("(","").replace(")","")
#re.sub(r"[\(\[].*?[\)\]]", "", line)
#line = line.strip().translate(None, "()")
line = line.strip()


#''.join(line.translate(string.maketrans("()[]"," "*4)).split(' ')[::2])
print(line)

#print(line.__class__)


#Get key/value
key, value = line.split('\t',1)




print ("key: " + str(key))
print ("Value: " + str(value))
#Parse key/value input (your code goes here)
# for val in value:
# if val[0] == "A":
# list_a.append(val)
# print(list_a)
#
#
# else:
# list_b.append(val)
# print(list_b)






#If we are still on the same key...
if key==currentkey:

#Process key/value pair (your code goes here)
for a in list_a:
#remove first two elems so that we're left with value
a = a[2:]
print(list_a)
result_a = list(map(int,result_a))
for b in list_b:
b = b[2:]
print(list_b)
result_b = list(map(int, result_b))
#multiply result_a and result_b for current key
result_ab = [a*b for a,b in zip(result_a,result_b)]
finalResult = sum(result_ab)

最佳答案

基本上,您将从元组中提取值
((0, 1), A 0 0.0)是一个元组,您可以通过tuple[0]tuple[1]等访问元组中的值...参见more examples here

这里line[0]((0,1))是另一个元组,因此我们需要将其转换为str以便输出最终结果
finalData = dataRDD.map(lambda line : str(line[0]) + "," + line[1])
测试:

>>> data = [((0, 1), 'A 0 0.0'), ((0, 2), 'A 0 0.0'), ((0, 0), 'A 0 0.0')]
>>> dataRDD = sc.parallelize(data)
>>> for i in dataRDD.collect():
... print(i)
...
((0, 1), 'A 0 0.0')
((0, 2), 'A 0 0.0')
((0, 0), 'A 0 0.0')
>>> finalData = dataRDD.map(lambda line : str(line[0]) + "," + line[1])
>>> for i in finalData.collect():
... print(i)
...
(0, 1),A 0 0.0
(0, 2),A 0 0.0
(0, 0),A 0 0.0
>>> finalData.saveAsTextFile('/user/cloudera/test123')
----------
$ hadoop fs -cat /user/cloudera/test123/*
(0, 1),A 0 0.0
(0, 2),A 0 0.0
(0, 0),A 0 0.0

关于python - 如何将此Python逗号分隔的字符串列表分成多个键值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42044775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com