最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Creating a value array from a list with varying size - Stack Overflow

programmeradmin1浏览0评论

I have the need to create a function which takes an input list or numpy array and creates into a string. The issue is easy enough to compute when knowing the numbers and the particulars. Like in the simple function code below, we see a list with 2x 100 which I can create the string very simply. I know that there are only 2 variables and can join simply to create the values using a and b.

squares = [(i, i) for i in range(100)]
values = ",".join([f"({a}, {b})" for (a, b) in squares])

Is there a way to create the values matrix if there was a np array with x by y dimensions? The y dimension will work through the loop easy enough, just the x. The x will change dependent on the particular need and list I am assessing - also the lists are variable with different types of data - mainly integers and floats.

I have the need to create a function which takes an input list or numpy array and creates into a string. The issue is easy enough to compute when knowing the numbers and the particulars. Like in the simple function code below, we see a list with 2x 100 which I can create the string very simply. I know that there are only 2 variables and can join simply to create the values using a and b.

squares = [(i, i) for i in range(100)]
values = ",".join([f"({a}, {b})" for (a, b) in squares])

Is there a way to create the values matrix if there was a np array with x by y dimensions? The y dimension will work through the loop easy enough, just the x. The x will change dependent on the particular need and list I am assessing - also the lists are variable with different types of data - mainly integers and floats.

Share Improve this question asked Mar 12 at 22:56 mhoopermhooper 1038 bronze badges 2
  • 1 could you show an example of array of size x-by-y and the expected output? How x varies? – ThomasIsCoding Commented Mar 12 at 23:06
  • I don't understand your problem. I don't see any x, y in your example. – furas Commented Mar 13 at 18:06
Add a comment  | 

1 Answer 1

Reset to default -1

So I had a coffee and found the a solution. This will create the array of values dependant on x and y.

import numpy as np

x = 5
y = 100
inputArray = np.zeros(x)

squares = [list(inputArray+i) for i in range(y)]
values = ','.join([f"{j}" for j in squares])
发布评论

评论列表(0)

  1. 暂无评论