将一个元组乘以标量

时间:2022-03-07 00:23:40

I have the following code:

我有以下代码:

print img.size
print 10 * img.size

this will print

这将打印

(70, 70)
(70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70)

while I'd like it to print

虽然我想要它打印

(700, 700)

Is any way there to do this without having to write

有没有办法做到这一点,而不必写

print (10 * img.size[0], 10 * img.size[1])

PS: img.size is a PIL image. Dunno if that matters anything in this case.

PS:img.size是一个PIL图像。 Dunno如果在这种情况下重要的话。

11 个解决方案

#1


36  

Might be a nicer way, but this should work

可能是一个更好的方式,但这应该工作

tuple([10*x for x in img.size])

#2


27  

img.size = tuple(i * 10 for i in img.size)

#3


13  

The pythonic way would be using a list comprehension:

pythonic方式将使用列表理解:

y = tuple([z * 10 for z in img.size])

Another way could be:

另一种方式可能是:

y = tuple(map((10).__mul__, img.size))

#4


7  

Solution:

解:

set1=(70, 70)
tuple(2*array(set1))

Explanation: arrays make direct scalar multiplication possible. Hence the tuple called set1 here is converted to an array. I assume you wish to keep using the tuple, hence we convert the array back to a tuple.

说明:数组可以直接进行标量乘法。因此,这里称为set1的元组将转换为数组。我假设您希望继续使用元组,因此我们将数组转换回元组。

This solution is to avoid the explicit and verbose for loop. I do not know whether it is faster or whether the exact same thing happens in both cases.

这个解决方案是避免显式和冗长的for循环。我不知道它是否更快或两种情况下是否完全相同。

#5


3  

There is probably a simpler way than this, but

可能有一种比这更简单的方法,但是

print map(lambda x: 10*x, img.size)

Will do nearly what you want, although it prints as a list rather than a tuple. Wrap the map call inside tuple(map...) if you want it to print as a tuple (parentheses rather than square brackets).

尽管它打印成列表而不是元组,但它几乎可以做你想要的。如果您希望将它打印为元组(括号而不是方括号),请将地图调用包含在元组内(map ...)。

#6


2  

If you have this problem more often and with larger tuples or lists then you might want to use the numpy library, which allows you to do all kinds of mathematical operations on arrays. However, in this simple situation this would be complete overkill.

如果你经常遇到这个问题并且有更大的元组或列表,那么你可能想要使用numpy库,它允许你对数组进行各种数学运算。但是,在这种简单的情况下,这将是完全矫枉过正的。

#7


1  

You can try something like this:

你可以尝试这样的事情:

print [10 * s for s in img.size]

It will give you a new list with all the elements you have in the tuple multiplied by 10

它将为您提供一个新列表,其中包含元组中的所有元素乘以10

#8


1  

In line with the previous answers but using numpy:

与之前的答案一致,但使用numpy:

import numpy as np
result = tuple(10*np.array(img.size))

#9


0  

adding nothing but variety..

只添加各种各样的..

import operator
img_size = (70, 70)
map(operator.mul, img_size, len(img_size)*(10,))

#10


0  

You are trying to apply the function on Tuple as a whole. You need to apply it on individual elements and return a new tuple.

您正在尝试将该函数作为一个整体应用于元组。您需要将它应用于单个元素并返回一个新元组。

newTuple = tuple([10*x for x in oldTuple])

Remember you cannot change a Tuple.

记住你不能改变一个元组。

#11


0  

Simplish thing if you're writing a bunch of code, but don't want a more complicated vector library...

如果您正在编写一堆代码,但不想要更复杂的矢量库,那就简单了...

class V(tuple):
    '''A simple vector supporting scalar multiply and vector add'''
    def __new__ (cls, *args):
        return super(V, cls).__new__(cls, args)
    def __mul__(self,s):
        return V( *( c*s for c in self) )
    def __add__(self,s):
        return V( *( c[0]+c[1] for c in zip(self,s)) )
    def __repr__(self):
        return "V" + super(V, self).__repr__()

# As long as the "vector" is on the left it just works

xaxis = V(1.0, 0.0)
yaxis = V(0.0, 1.0)
print xaxis + yaxis      # => V(1.0, 1.0)
print xaxis*3 + yaxis*5  # => V(3.0, 5.0)
print 3*xaxis            # Broke, => (1.0, 0.0, 1.0, 0.0, 1.0, 0.0)

The "V" instances otherwise behave just like tuples. This requires that the "V" instances are all created with the same number of elements. You could add, for example, to __new__

否则“V”实例的行为就像元组一样。这要求“V”实例都使用相同数量的元素创建。例如,您可以添加到__new__

if len(args)!=2: raise TypeError('Must be 2 elements')

to enforce that all the instances are 2d vectors....

强制所有实例都是2d向量....

#1


36  

Might be a nicer way, but this should work

可能是一个更好的方式,但这应该工作

tuple([10*x for x in img.size])

#2


27  

img.size = tuple(i * 10 for i in img.size)

#3


13  

The pythonic way would be using a list comprehension:

pythonic方式将使用列表理解:

y = tuple([z * 10 for z in img.size])

Another way could be:

另一种方式可能是:

y = tuple(map((10).__mul__, img.size))

#4


7  

Solution:

解:

set1=(70, 70)
tuple(2*array(set1))

Explanation: arrays make direct scalar multiplication possible. Hence the tuple called set1 here is converted to an array. I assume you wish to keep using the tuple, hence we convert the array back to a tuple.

说明:数组可以直接进行标量乘法。因此,这里称为set1的元组将转换为数组。我假设您希望继续使用元组,因此我们将数组转换回元组。

This solution is to avoid the explicit and verbose for loop. I do not know whether it is faster or whether the exact same thing happens in both cases.

这个解决方案是避免显式和冗长的for循环。我不知道它是否更快或两种情况下是否完全相同。

#5


3  

There is probably a simpler way than this, but

可能有一种比这更简单的方法,但是

print map(lambda x: 10*x, img.size)

Will do nearly what you want, although it prints as a list rather than a tuple. Wrap the map call inside tuple(map...) if you want it to print as a tuple (parentheses rather than square brackets).

尽管它打印成列表而不是元组,但它几乎可以做你想要的。如果您希望将它打印为元组(括号而不是方括号),请将地图调用包含在元组内(map ...)。

#6


2  

If you have this problem more often and with larger tuples or lists then you might want to use the numpy library, which allows you to do all kinds of mathematical operations on arrays. However, in this simple situation this would be complete overkill.

如果你经常遇到这个问题并且有更大的元组或列表,那么你可能想要使用numpy库,它允许你对数组进行各种数学运算。但是,在这种简单的情况下,这将是完全矫枉过正的。

#7


1  

You can try something like this:

你可以尝试这样的事情:

print [10 * s for s in img.size]

It will give you a new list with all the elements you have in the tuple multiplied by 10

它将为您提供一个新列表,其中包含元组中的所有元素乘以10

#8


1  

In line with the previous answers but using numpy:

与之前的答案一致,但使用numpy:

import numpy as np
result = tuple(10*np.array(img.size))

#9


0  

adding nothing but variety..

只添加各种各样的..

import operator
img_size = (70, 70)
map(operator.mul, img_size, len(img_size)*(10,))

#10


0  

You are trying to apply the function on Tuple as a whole. You need to apply it on individual elements and return a new tuple.

您正在尝试将该函数作为一个整体应用于元组。您需要将它应用于单个元素并返回一个新元组。

newTuple = tuple([10*x for x in oldTuple])

Remember you cannot change a Tuple.

记住你不能改变一个元组。

#11


0  

Simplish thing if you're writing a bunch of code, but don't want a more complicated vector library...

如果您正在编写一堆代码,但不想要更复杂的矢量库,那就简单了...

class V(tuple):
    '''A simple vector supporting scalar multiply and vector add'''
    def __new__ (cls, *args):
        return super(V, cls).__new__(cls, args)
    def __mul__(self,s):
        return V( *( c*s for c in self) )
    def __add__(self,s):
        return V( *( c[0]+c[1] for c in zip(self,s)) )
    def __repr__(self):
        return "V" + super(V, self).__repr__()

# As long as the "vector" is on the left it just works

xaxis = V(1.0, 0.0)
yaxis = V(0.0, 1.0)
print xaxis + yaxis      # => V(1.0, 1.0)
print xaxis*3 + yaxis*5  # => V(3.0, 5.0)
print 3*xaxis            # Broke, => (1.0, 0.0, 1.0, 0.0, 1.0, 0.0)

The "V" instances otherwise behave just like tuples. This requires that the "V" instances are all created with the same number of elements. You could add, for example, to __new__

否则“V”实例的行为就像元组一样。这要求“V”实例都使用相同数量的元素创建。例如,您可以添加到__new__

if len(args)!=2: raise TypeError('Must be 2 elements')

to enforce that all the instances are 2d vectors....

强制所有实例都是2d向量....