`
messi_18
  • 浏览: 97009 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

list comprehension in python

阅读更多
list:
points=[('118.696', '55.016'), ('64.583', '195.986'), ('229.826', '259.417'), ('283.94', '118.447')]


list comprehension:
[Decimal(x),Decimal(y) for x,y in points]

SyntaxError: invalid syntax

[(Decimal(x),Decimal(y)) for x,y in points]


Conclusion:
if you want to return a tuple, you have to speak it out:-)

another list comprehension:
for i,x,y in points:

result:SyntaxError: invalid syntax

try again:
for i,(x,y) in points:


conclusion:if you want to get a tuple, you'd better not put it next to others, here is index i, instead make it clear that it's a tuple.

分享到:
评论

相关推荐

    Python进阶内容 List Comprehension _python_

    目前中文资料中讲解列表推导式最清晰的文档

    python基础教程:Python中在for循环中嵌套使用if和else语句的技巧.pdf

    Python的语法糖⾮常强⼤,⽐如Python中在for循环中嵌套使⽤if和else语句的技巧便⼗分给⼒,下⾯我们就举⼏个例⼦来看详细的⽤法: for…[if]…构建List (List comprehension) 1.简单的for…[if]…语句 Python中,for...

    Python-16-20题.pdf

    Python-16~20题 Python-16~20题 ---16--- ---16--- Question: Question: >Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma- >Use a list comprehension...

    python循环语句嵌套-python基础教程:Python中在for循环中嵌套使用if和 .pdf

    Python的语法糖⾮常强⼤,⽐如Python中在for循环中嵌套使⽤if和else语句的技巧便⼗分给⼒,下⾯我们就举⼏个例⼦来看详细的⽤法: for…[if]…构建List (List comprehension) 1.简单的for…[if]…语句 Python中,for...

    python中的list,set,dict comprehension详解

    这其实是python中提供的遍历、筛选一个序列(如list,set,dict)中元素的特殊语法,其本质为在for loop 中对一个序列中元素执行指定的操作,要实现一个comprehension语法,我们需要确定四个要素:目标序列,目标...

    python基础教程:Python中在for循环中嵌套使用if和else语句的技巧

    for…[if]…构建List (List comprehension) 1.简单的for…[if]…语句 Python中,for…[if]…语句一种简洁的构建List的方法,从for给定的List中选择出满足if条件的元素组成新的List,其中if是可以省略的。下面举几...

    《Python One-Liners》 by Christian Mayer .pdf

    Following a brief Python refresher, the book covers essential advanced topics like slicing, list comprehension, broadcasting, lambda functions, algorithms, regular expressions, neural networks, ...

    python中lambda函数 list comprehension 和 zip函数使用指南

    Python 支持一种有趣的语法,它允许你快速定义单行的最小函数。这些叫做 lambda 的函数,是从 Lisp 借用来的,可以用在任何需要函数的地方。 def f(x): return x*2,用lambda函数来替换可以写成:g = lambda x: x*2`...

    python面试题合集.pdf

    请解释一下 Python 的列表推导式(List Comprehension)和字典推导式(Dictionary Comprehension)及其语法。 - 7. 介绍一下 Python 中的面向对象编程(Object-Oriented Programming,简称 OOP)的概念和特点。 - 8...

    Python Cookbook, 2nd Edition

    Interpolating Variables in a Stringin Python 2.4 Recipe 1.18. Replacing Multiple Patterns in a Single Pass Recipe 1.19. Checking a String for Any of Multiple Endings Recipe 1.20. Handling ...

    Beginning Python (2005).pdf

    Implementing a Search Utility in Python 200 Try It Out: Writing a Test Suite First 201 Try It Out: A General-Purpose Search Framework 203 A More Powerful Python Search 205 Try It Out: Extending ...

    python开发之list操作实例分析

    本文实例分析了python开发之list操作。分享给大家供大家参考,具体如下: 对python中list的操作,... 3.Using a list comprehension:[x for x in iterable] 4.Using the type constructor:list() or list(iterable) '

    Python中在for循环中嵌套使用if和else语句的技巧

    for…[if]…构建List (List comprehension) 1.简单的for…[if]…语句 Python中,for…[if]…语句一种简洁的构建List的方法,从for给定的List中选择出满足if条件的元素组成新的List,其中if是可以省略的。下面举几...

    Python for Bioinformatics 第二版,最新版

    3.2.3 List Comprehension 46 3.2.4 Modifying Lists 47 3.2.5 Copying a List 49 3.3 TUPLES 49 3.3.1 Tuples Are Immutable Lists 49 3.4 COMMON PROPERTIES OF THE SEQUENCES 51 3.5 DICTIONARIES 54 3.5.1 ...

    a_byte_of_python

    List Comprehension Using List Comprehensions Receiving Tuples and Lists in Functions Lambda Forms Using Lambda Forms The exec and eval statements The assert statement The repr function Summary...

    python中的列表推导浅析

    列表推导(List comprehension)的作用是为了更方便地生成列表(list)。 比如,一个list变量的元素均为数字,如果需要将每个元素的值乘以2并生成另外一个list,下面是一种做法:复制代码 代码如下:#-*-encoding:utf...

    Python高效编程技巧

    如果你对list comprehensions概念不是很熟悉——一个list comprehension就是一个更简短、简洁的创建一个list的方法。 >>> some_list = [1, 2, 3, 4, 5] >>> another_list = [ x + 1 for x in some_

    在python中创建指定大小的多维数组方式

    python中创建指定大小的二维数组,有点像C++中进行...当然也可以使用list comprehension的方式创建: n = 2 m = 3 matrix = [[0]*m for i in range(n)] print(matrix) matrix[0][0] = 1 print(matrix) 对于创建三维

    Python 列表推导式需要注意的地方

    原文地址:The Do’s and Don’ts of Python List Comprehension 原文作者:Yong Cui, Ph.D. 译文出自:掘金翻译计划 本文永久链接:github.com/xitu/gold-m… 译者:samyu2000 校对者:luochen1992,shixi-li ...

Global site tag (gtag.js) - Google Analytics