简体   繁体   English

将字符串元组编码为整数

[英]Encoding a tuple of strings into integers

I'm trying to write by first Neural network from scratch using 2 inputs (length and width) to give the output (r/b). 我正在尝试使用2个输入(长度和宽度)从头开始从第一个神经网络编写输出(r / b)。 I want to encode the strings r and b into integers 0 and 1 so that I have an output vector. 我想将字符串r和b编码为整数0和1,以便有一个输出向量。 Without using a pre-made function like labelenconder. 不使用像labelenconder这样的预制函数。

length = np.array((3,2,4,3,3.5,2,5.5,1,4.5)) width = np.array((1.5,1,1.5,1,.5,.5,1,1,1)) c =('r','b','r','b','r','b','r','b','?')

Thanks in advance 提前致谢

You can do this fairly quickly like so: 您可以像这样快速完成此操作:

my_matcher = {'r':0, 'b':1}

new_input = [my_matcher.get(val) for val in c]

If you need it to be a tuple, you can wrap it with tuple(new_input) 如果需要将其作为元组,则可以将其包装为tuple(new_input)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将字符串列表转换为整数元组 - Convert list of strings into tuple of integers 将整数和字符串的元组转换为字符串 - Converting Tuple of integers and strings to just a string 在 memcached 中安全存储 Python 字符串和整数元组的最佳方法? - Best way to safely store Python tuple of strings and integers in memcached? 如何在python for循环中构建整数+字符串的元组数组变量? - How to build tuple array variable of integers + strings in python for loop? Bert 标记错误 ValueError:输入 nan 无效。 应该是字符串、字符串列表/元组或整数列表/元组 - Bert Tokenizing error ValueError: Input nan is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers “输入无效。 应该是字符串、字符串列表/元组或整数列表/元组。” ValueError:输入无效 - “Input is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers.” ValueError: Input is not valid BertTokenizer 错误 ValueError:输入 nan 无效。 应该是字符串、字符串列表/元组或整数列表/元组 - BertTokenizer error ValueError: Input nan is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers 与整数的元组比较 - Tuple Comparison with Integers 整数元组的条件 - condition of tuple of integers 将对象元组转换为字符串元组 - Convert a tuple of objects to a tuple of strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM