tuples

Why in Python does “0, 0 == (0, 0)” equal “(0, False)”?

ぃ、小莉子 提交于 2019-11-30 10:13:59
问题 In Python (I checked only with Python 3.6 but I believe it should hold for many of the previous versions as well): (0, 0) == 0, 0 # results in a two element tuple: (False, 0) 0, 0 == (0, 0) # results in a two element tuple: (0, False) (0, 0) == (0, 0) # results in a boolean True But: a = 0, 0 b = (0, 0) a == b # results in a boolean True Why does the result differ between the two approaches? Does the equality operator handle tuples differently? 回答1: The first two expressions both parse as

getting error when converting a pyscript to exe using Py2Exe

一笑奈何 提交于 2019-11-30 09:51:26
问题 I am new to this py2exe, I have my script in cmd.py import sys for arg in sys.argv: print arg print "Hello World!" setup.py, from distutils.core import setup import py2exe setup(console=['cmd.py']) having both the files in same directory, I ran, python setup.py py2exe and getting the below error, G:\PyScripts>python setup.py py2exe running py2exe Traceback (most recent call last): File "setup.py", line 3, in <module> setup(console=['cmd.py']) File "C:\ProgramData\Anaconda3\lib\distutils\core

What does the comma in this assignment statement do?

筅森魡賤 提交于 2019-11-30 09:33:26
问题 I was looking through an interesting example script I found (at this site, last example line 124), and I'm struggling to understand what the comma after particles achieves in this line: particles, = ax.plot([], [], 'bo', ms=6) The script will hit an error if the comma is omitted, but the syntax (which seems to resemble an unpacking statement) does not make much sense to me, and a statement like a, = [2,3] fails, which seems like an argument against the unpacking theory. Any insight would be

Emit Tuples From Erlang Views In CouchDB

家住魔仙堡 提交于 2019-11-30 09:10:45
CouchDB, version 0.10.0, using native erlang views. I have a simple document of the form: { "_id": "user-1", "_rev": "1-9ccf63b66b62d15d75daa211c5a7fb0d", "type": "user", "identifiers": [ "ABC", "DEF", "123" ], "username": "monkey", "name": "Monkey Man" } And a basic javascript design document: { "_id": "_design/user", "_rev": "1-94bd8a0dbce5e2efd699d17acea1db0b", "language": "javascript", "views": { "find_by_identifier": { "map": "function(doc) { if (doc.type == 'user') { doc.identifiers.forEach(function(identifier) { emit(identifier, {\"username\":doc.username,\"name\":doc.name}); }); } }" }

Return more than one value from a function in Java

感情迁移 提交于 2019-11-30 08:42:50
问题 How to return more than one value from a function in Java? Can anyone give sample code for doing this using tuples? I am not able to understand the concept of tuples. public class Tuple{ public static void main(String []args){ System.out.println(f()); } static Pair<String,Integer> f(){ return new Pair<String,Integer>("hi",3); } public class Pair<String,Integer> { public final String a; public final Integer b; public Pair(String a, Integer b) { this.a = a; this.b = b; } } } What is the mistake

Is this expected C# 4.0 Tuple equality behavior?

别说谁变了你拦得住时间么 提交于 2019-11-30 08:42:20
I'm seeing different behavior between using .Equals and == between two of .NET 4.0's new Tuple<> instances. If I have overridden Equals on the object in the Tuple<> and call .Equals on the Tuples the override of Equals will be called. If I use == on the Tuples the override of Equals is not called. Is that by design and does it make sense? EDIT: From answers and comments I can tell I'm not being clear. I know Tuple<> is a reference type and that for reference types == will check identity (ReferenceEquals). But, should Tuple<> override == to check equality of the objects it contains? For

Tuple as index of multidimensional array

半城伤御伤魂 提交于 2019-11-30 08:37:48
问题 I found a very similar question to mine, but not exactly the same. This one: here However in ntimes's case the size of the array matches the number of the dimensions the tuple is point at. In my case I have a 4-dimensional array and a 2-dimensional tuple, just like this: from numpy.random import rand big_array=rand(3,3,4,5) tup=(2,2) I want to use the tuple as an index to the first two dimensions, and manually index the last two. Something like: big_array[tup,3,2] However, I obtain a

Sort Tuples Python

强颜欢笑 提交于 2019-11-30 08:13:08
I have a list of tuples in my Blender python code scores=[(1489,"Sean"), (2850,"Bob"), (276,"Crap Player"), (78495, "Great Player"), (8473, "Damian"), (4860, "Andy"), (0, "Stephen")] I'm trying to sort them by their score by using this sorted(scores, key=lambda score: score[0], reverse=True) but this is not working. I have no idea why. Any tips? I've considered maybe a better implementation is to create a new Score class with fields name and score EDIT: Thanks guys for the fast reply it was giving me no errors with the sorted method but was not sorting. I used the sort() and it works. I think

How can I handle a > 22 column table with Slick using nested tuples or HLists?

眉间皱痕 提交于 2019-11-30 08:09:01
I'm new to Scala (using 2.10) and Slick (using 2.0-M2). I see that one of the ways to get around the 22 column limit for tables in Slick is to use nested tuples. I can't figure out how to do that, despite finding this partial code on GitHub . Current dev branch Scala (2.11-M5) supports case classes with more than 22 elements, but not tuples with arity > 22. And Slick is not yet distributed for Scala 2.11 pre-releases. How can I define a 33 column table (and have it work with all Slick's syntactic sugar)? N.B., I'm trying to support an existing schema and can't change the table normalization.

Changing values of a list of namedtuples

对着背影说爱祢 提交于 2019-11-30 07:59:07
I have a list of namedtuples named Books and am trying to increase the price field by 20% which does change the value of Books . I tried to do: from collections import namedtuple Book = namedtuple('Book', 'author title genre year price instock') BSI = [ Book('Suzane Collins','The Hunger Games', 'Fiction', 2008, 6.96, 20), Book('J.K. Rowling', "Harry Potter and the Sorcerer's Stone", 'Fantasy', 1997, 4.78, 12)] for item in BSI: item = item.price*1.10 print(item.price) But I keep getting : Traceback (most recent call last): print(item.price) AttributeError: 'float' object has no attribute 'price