python输入多个值用逗号隔开_如何将这个Python逗号分隔的字符串列表分成多个键值对?...

如果我有表格的输入( (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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值