英文:
Converting integers in a list to list of digits
问题
I can help you with the translation. Here's the translated content:
我正在解决一个输入问题:
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
期望的输出:
[1, 11, 121, 1331, 14641]
那么,是否有办法使用Python3将我的数组转换为期望的输出?
我的代码:
numRows = 5
li = list([str(11**i) for i in range(numRows)])
li = [int("".join(i)) for i in li]
我尝试了几种方法,但未能达到期望的输出。
英文:
I was solving a problem with input:
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Desired output:
[1,11,121,1331,14641]
So is there any way to convert my array to the desired output using python3
My code:
numRows=5
li=list([str(11**i )for i in range(numRows)])
li=[int("".join(i))for i in li]
I tried several ways but was not able to reach the desired output
答案1
得分: 1
为什么要在可以用算术方法解决时转换为/从字符串?
lol = [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
output = []
for e in lol:
n = 0
for v in e:
n = n * 10 + v
output.append(n)
print(output)
输出:
[1, 11, 121, 1331, 14641]
英文:
Why convert to/from strings when this can be done arithmetically?
lol = [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
output = []
for e in lol:
n = 0
for v in e:
n = n * 10 + v
output.append(n)
print(output)
Output:
[1, 11, 121, 1331, 14641]
答案2
得分: 0
Here is the translated code without the translation of code comments:
input_array = [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
output_array = []
for row in input_array:
row_output = [str(num) for num in row]
row_output = ''.join(row_output)
row_output = int(row_output)
output_array.append(row_output)
print(output_array)
英文:
input_array = [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
output_array = []
for row in input_array:
row_output = [str(num) for num in row]
row_output = ''.join(row_output)
row_output = int(row_output)
output_array.append(row_output)
print(output_array)
答案3
得分: 0
Regarding your second attempted method, it is very close to working. The str.join
method only works on lists of strings, but you are attempting to use it on a list of integers. This can be fixed by simply converting the list to all strings:
li=[int("".join(map(str, i))) for i in li]
英文:
Regarding your second attempted method, it is very close to working. The str.join
method only works on lists of strings, but you are attempting to use it on a list of integers.
This can be fixed by simply converting the list to all strings:
li=[int("".join(map(str, i))) for i in li]
答案4
得分: 0
以下是翻译好的部分:
def concat_list_digits(ldigits):
for i, item in enumerate(ldigits):
curr_len = len(item)
value = 0
for i in range(curr_len):
value += item[i] * (10 ** (curr_len - i - 1))
ldigits[i] = value
return ldigits
if __name__ == "__main__":
digits = [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
print(concat_list_digits(digits))
英文:
Here is a math based solution, without relying on int to str conversion.
Of course, you can use list comprehension, but I've just kept it simple.
def concat_list_digits(ldigits):
for i, item in enumerate(ldigits):
curr_len = len(item)
value = 0
for i in range(curr_len):
value += item[i] * (10 ** (curr_len - i - 1))
ldigits[i] = value
return ldigits
if __name__ == "__main__":
digits = [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
print(concat_list_digits(digits))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论