标签: iterable-unpacking

初始化大量变量的最简单方法

假设您有大量需要初始化为None的变量.一个天真的方法是计算左边的变量数量,并在右边创建一个相同大小的列表:

a, b, c, d, e, f, g, h, i, j = [None]*10
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点,而不必计算变量?如果经常使用这种模式,那么必须计算变量的数量会变得很繁琐.

python initialization iterable-unpacking

3
推荐指数
2
解决办法
390
查看次数

元组在作业中打开包装

我想在python声明中解压缩一个元组,如下所示:

a = 5, *(6,7)
Run Code Online (Sandbox Code Playgroud)

但这提出了一个SyntaxError.实现这样的目标最简洁的方法是什么?

到目前为止我提出的最好的是:

a = tuple([5]+list((6,7)))
Run Code Online (Sandbox Code Playgroud)

python tuples iterable-unpacking

3
推荐指数
1
解决办法
305
查看次数

std :: tuple到成员函数

我仍然试图摆脱元编程,我很难过.

我想做的是创建一个类/ struct/whatever,为它提供一个std :: tuple并让它根据元组中的对象类型自动生成成员函数.目标是让类派生自MessageHandler

例如

typedef std::tuple< MessageA, MessageB, MessageC > MessageSet;

template< class T >
class MessageHandler
{
  // some magic metaprogramming would "create"...
  virtual void processMsg( const MessageA& ) = 0;
  virtual void processMsg( const MessageB& ) = 0;
  virtual void processMsg( const MessageC& ) = 0;
};
Run Code Online (Sandbox Code Playgroud)

我已经读过你不能在模板中使用虚函数,但我不知道C++ 11是否仍然如此.

谢谢.

c++ templates virtual-functions variadic-templates iterable-unpacking

3
推荐指数
1
解决办法
334
查看次数

如果我开始返回更多数据,则返回可解包值的可迭代值,这些值不会破坏客户端代码

假设你正在编写一个你打算公开的API.函数quux的API中返回一个列表或元组的发电机,例如yield (foo, bar).

客户端代码通常会像这样使用它:

for foo, bar in quux(whatever):
    # do stuff with foo and bar
Run Code Online (Sandbox Code Playgroud)

现在,假设将来你可能想和and baz一起开始回来.你现在不想退货,因为YAGNI直到证明不然.foobar

什么是最好的方法(尝试)确保未来的更改不会破坏客户端代码?

我知道Python 3允许人们做类似的事情for foo, bar, *idontcare in quux(whatever),在Python 2中,总是可以编写一个实用程序函数(使用这样for foo, bar in iterleft(quux(whatever), 2)):

def iterleft(iterable, limit):
    for item in iterable:
        yield item[:limit]
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有更好的方法来做这样的事情.

python iterable-unpacking

3
推荐指数
1
解决办法
181
查看次数

仅将一些元组元素展开到变量列表

说我有以下代码:

from urlparse import urlparse
parsed_url = urlparse(url)
scheme, netloc, path = parsed_url[0], parsed_url[1], parsed_url[2]
Run Code Online (Sandbox Code Playgroud)

是否有更优雅或简短的方式来分配这三个变量?写parsed_url三次看起来有点乱(我期待除了重命名之外的其他parsed_url东西).

python iterable-unpacking

3
推荐指数
1
解决办法
715
查看次数

尝试解压缩集时出现TypeError与ValueError

为什么跟随两个代码片段会产生不同的错误?我理解字符串是可迭代的,但是我不明白为什么这在这里很重要,因为集合是被迭代的对象.

s = set([1, 2])        
for one, two in s:
  print one, two
Run Code Online (Sandbox Code Playgroud)

加薪

Traceback (most recent call last):
  File "asdf.py", line 86, in <module>
    for one, two in s:
TypeError: 'int' object is not iterable
Run Code Online (Sandbox Code Playgroud)
s2 = set(['a', 'b'])
for one, two in s2:
  print one, two 
Run Code Online (Sandbox Code Playgroud)

加薪

Traceback (most recent call last):
  File "asdf.py", line 90, in <module>
    for one, two in s2:
ValueError: need more than 1 value to unpack
Run Code Online (Sandbox Code Playgroud)

python iterable-unpacking

3
推荐指数
1
解决办法
3259
查看次数

Python:从数组中分配变量

我在Python中有一些代码:

repost_pid = row[0]
repost_permalink = row[1]
repost_domain = row[2]
repost_title = row[3]
repost_submitter = row[4]
Run Code Online (Sandbox Code Playgroud)

是否有一种单行方式来分配这些变量?

另外,如果我想跳过一个值,我该怎么办?

python list iterable-unpacking

3
推荐指数
1
解决办法
1860
查看次数

首先进行解构/拆包并在Clojure中休息

在Python 3中,如果要解压缩列表(或元组)的第一个和其余部分,则可以

x, *y = [1, 2, 3]
#x = 1, y = [2, 3]
Run Code Online (Sandbox Code Playgroud)

你如何在Clojure中的let块中执行此操作?我试着:as parts

(defn destructurer [vec]
  (let [[beginning the-rest :as parts] vec]
    [beginning the-rest]
    )
  )
;; (destructer [1 2 3])
;; [1 2] <- missing the 3
Run Code Online (Sandbox Code Playgroud)

clojure python-3.x iterable-unpacking

3
推荐指数
1
解决办法
275
查看次数

将元组直接解压缩到scala中的类

Scala能够在执行各种操作时将元组解压缩为多个局部变量,例如,如果我有一些数据

val infos = Array(("Matt", "Awesome"), ("Matt's Brother", "Just OK"))
Run Code Online (Sandbox Code Playgroud)

而不是做一些丑陋的事情

infos.map{ person_info => person_info._1 + " is " + person_info._2 }
Run Code Online (Sandbox Code Playgroud)

我可以选择更优雅

infos.map{ case (person, status) => person + " is " + status }
Run Code Online (Sandbox Code Playgroud)

我经常想知道的一件事是如何将元组直接解压缩到比如在类构造函数中使用的参数.我想象的是这样的:

case class PersonInfo(person: String, status: String)
infos.map{ case (p: PersonInfo) => p.person + " is " + p.status }
Run Code Online (Sandbox Code Playgroud)

如果PersonInfo有方法,甚至更好:

infos.map{ case (p: PersonInfo) => p.verboseStatus() }
Run Code Online (Sandbox Code Playgroud)

但当然这不起作用.如果已经有人问过这个道歉 - 我还没有找到直接答案 - 有没有办法做到这一点?

scala iterable-unpacking

3
推荐指数
1
解决办法
692
查看次数

为什么"in"适用于键而不适用于字符串?

今天我在Python中遇到了一个非常奇怪的字符串和字典.有人可以向我解释为什么print语句在第一个for循环中工作但在第二个for循环中失败?

test = 'ab'
test_dict = {}
test_dict[test] = 1

for x, y in test_dict:
    print('%s %s' % (x,y))

for x,y in test:
    print('%s %s' % (x,y))
Run Code Online (Sandbox Code Playgroud)

python string dictionary iterable-unpacking

3
推荐指数
2
解决办法
80
查看次数