I have a pretty simple idea in mind but don't exactly know how to go about doing it. I have two lists of different length (say 50 in one and 200 in the other).
我有一个非常简单的想法,但不知道如何去做。我有两个不同长度的列表(比如50合1和200另一个)。
so
len(x) = 50
len(y) = 200
What I want to do is print out some lines where one value from x corresponds to multiple values of y (does not matter if numbers are repeated).
我想要做的是打印出一些行,其中x中的一个值对应于y的多个值(如果重复数字则无关紧要)。
I.e. suppose x = [1,2,3,4]
y = [7,8,9,10,11,12,13,14,15,16,17]
I want to print out:
1: 7
1: 8
1: 9
1: 10
2: 11
2: 12
2: 13
3: 14
3: 15
3: 16
4: 17
Randomization of values from y does not really matter. It does not matter how many multiple values get printed out for each value of x. I am basically trying to write a sql script of inserting multiple elements from one table into another and using python to write the script.
来自y的值的随机化并不重要。对于x的每个值,打印出多少个值并不重要。我基本上试图编写一个sql脚本,从一个表插入多个元素到另一个表,并使用python编写脚本。
I know this can be done by simply iterating over the smallest list and having multiple f.write() statements while incrementing some variable to have multiple values of y associated to a single value of x. But this does not cover all the values of y and looks kind of silly. Is there a better way to go about doing this?
我知道这可以通过简单地迭代最小的列表并具有多个f.write()语句来完成,同时递增一些变量以使y的多个值与单个x值相关联。但这并没有涵盖y的所有价值,看起来有点傻。有没有更好的方法来做这件事?
Edit: For the question where how I came up with the association of keys to values, it does not really matter how many values 1 key is associated with (an upper bound of 8 exists). There can be only 1 value or multiple (up to 8). Then again, there can be repetitions of values between keys (i.e. in my example there is not but key 4 can say have values 7/8 also)
编辑:对于我如何想出键值关联的问题,与1个键关联的值(8的上限存在)并不重要。只能有1个值或多个值(最多8个)。然后,键之间可能会重复出现值(即在我的例子中没有,但键4也可以说有值7/8)
2 个解决方案
#1
0
Try this Python solution:
试试这个Python解决方案:
>>> x = [1,2,3,4]
>>> y = [7,8,9,10,11,12,13,14,15,16,17]
>>> lnx, lny = len(x), len(y)
>>> groupatleast = (lny + lnx - 1) // lnx
>>> x2y = {}
>>> for i in range(lnx):
x2y[x[i]] = y[i*groupatleast: (i+1)*groupatleast]
>>> x2y[x[i]] += y[(i+1)*groupatleast:]
>>> for x1,ylist in sorted(x2y.items()):
for y1 in ylist:
print ("%i: %i" % (x1, y1))
1: 7
1: 8
1: 9
2: 10
2: 11
2: 12
3: 13
3: 14
3: 15
4: 16
4: 17
>>>
>>> # Help:
>>> lnx, lny
(4, 11)
>>> groupatleast
3
>>> x2y
{1: [7, 8, 9], 2: [10, 11, 12], 3: [13, 14, 15], 4: [16, 17]}
>>> ylist
[16, 17]
>>>
#2
0
>>> xs = [1,2,3,4]
>>> ys = [7,8,9,10,11,12,13,14,15,16,17]
>>> for (i, y) in enumerate(ys):
>>> print "%d: %d" % (xs[len(xs) * i / len(ys)], y)
1: 7
1: 8
1: 9
2: 10
2: 11
2: 12
3: 13
3: 14
3: 15
4: 16
4: 17
>>> import itertools
>>> for x, y in itertools.izip(itertools.cycle(xs), ys):
... print "%d: %d" % (x, y)
...
1: 7
2: 8
3: 9
4: 10
1: 11
2: 12
3: 13
4: 14
1: 15
2: 16
3: 17
#1
0
Try this Python solution:
试试这个Python解决方案:
>>> x = [1,2,3,4]
>>> y = [7,8,9,10,11,12,13,14,15,16,17]
>>> lnx, lny = len(x), len(y)
>>> groupatleast = (lny + lnx - 1) // lnx
>>> x2y = {}
>>> for i in range(lnx):
x2y[x[i]] = y[i*groupatleast: (i+1)*groupatleast]
>>> x2y[x[i]] += y[(i+1)*groupatleast:]
>>> for x1,ylist in sorted(x2y.items()):
for y1 in ylist:
print ("%i: %i" % (x1, y1))
1: 7
1: 8
1: 9
2: 10
2: 11
2: 12
3: 13
3: 14
3: 15
4: 16
4: 17
>>>
>>> # Help:
>>> lnx, lny
(4, 11)
>>> groupatleast
3
>>> x2y
{1: [7, 8, 9], 2: [10, 11, 12], 3: [13, 14, 15], 4: [16, 17]}
>>> ylist
[16, 17]
>>>
#2
0
>>> xs = [1,2,3,4]
>>> ys = [7,8,9,10,11,12,13,14,15,16,17]
>>> for (i, y) in enumerate(ys):
>>> print "%d: %d" % (xs[len(xs) * i / len(ys)], y)
1: 7
1: 8
1: 9
2: 10
2: 11
2: 12
3: 13
3: 14
3: 15
4: 16
4: 17
>>> import itertools
>>> for x, y in itertools.izip(itertools.cycle(xs), ys):
... print "%d: %d" % (x, y)
...
1: 7
2: 8
3: 9
4: 10
1: 11
2: 12
3: 13
4: 14
1: 15
2: 16
3: 17