tuples

What to pass when passing arguments where a list or tuple is required?

旧城冷巷雨未停 提交于 2019-12-11 00:01:38
问题 Which of the following should I use and why? import numpy as np a = np.zeros([2, 3]) b = np.zeros((2, 3)) There are many cases where you can pass arguments in either way, I just wonder if one is more Pythonic or if there are other reasons where one should be preferred over the other. I looked at this question where people tried to explain the difference between a tuple and a list. That's not what I'm interested in, unless there are reasons I should care which I ignore of course! UPDATE:

NumPy: using loadtxt or genfromtxt to read a ragged structure

心已入冬 提交于 2019-12-10 23:58:21
问题 I need to read an ASCII file into Python, where an excerpt of the file looks like this: E M S T N... ... 9998 1 1 128 10097 10098 10199 10198 20298 20299 20400 20399 9999 1 1 128 10098 10099 10200 10199 20299 20300 20401 20400 10000 1 1 128 10099 10100 10201 10200 20300 20301 20402 20401 10001 1 2 44 2071 2172 12373 12272 10002 1 2 44 2172 2273 12474 12373 The above should ideally be following NumPy schema: array([(9998, 1, 1, 128, (10097, 10098, 10199, 10198, 20298, 20299, 20400, 20399)),

How do I split in Pig a tuple of many maps into different rows

喜夏-厌秋 提交于 2019-12-10 23:41:40
问题 I have a relation in Pig that looks like this: ([account_id#100, timestamp#1434, id#900], [account_id#100, timestamp#1434, id#901], [account_id#100, timestamp#1434, id#902]) As you can see, I have three map objects within a tuple. All of the data above is within the $0'th field in the relation. So the data above in a relation with a single bytearray column. The data is loaded as follows: data = load 's3://data/data' using com.twitter.elephantbird.pig.load.JsonLoader('-nestedLoad'); DESCRIBE

Counting items inside tuples in Python

北城以北 提交于 2019-12-10 22:36:58
问题 I am fairly new to python and I could not figure out how to do the following. I have a list of (word, tag) tuples a = [('Run', 'Noun'),('Run', 'Verb'),('The', 'Article'),('Run', 'Noun'),('The', 'DT')] I am trying to find all tags that has been assigned to each word and collect their counts. For example, word "run" has been tagged twice to 'Noun' and once to 'Verb'. To clarify: I would like to create another list of tuples that contains (word, tag, count) 回答1: You can use collections.Counter:

std::forward_as_tuple to pass arguments to 2 constructors

混江龙づ霸主 提交于 2019-12-10 21:46:48
问题 I would like to pass multiple arguments in order to construct two objects inside a function, the same way std::pair<T1, T2>(std::piecewise_construct, ...) works. So I wrote template <typename Args0..., typename Args1...> void f(std::tuple<Arg0> args0, std::tuple<Args1> args1) { Object0 alpha(...); Object1 beta(...); ... } so I can call f(std::forward_as_tuple(..., ..., ...), std::forward_as_tuple(..., ...)) But I don't know how to construct Object0 and Object1 . I have checked the source code

How to convert single element tuple into string?

荒凉一梦 提交于 2019-12-10 21:24:21
问题 I have this code: import nltk import pypyodbc text = raw_input() token = nltk.word_tokenize(text) //return a list value def search(self, lists): if not self.connected: self.connect() for word in lists: self.cur.execute('SELECT Ybanag FROM Words WHERE English IN (%s)' % (','.join('?'*len(lists))), lists) result = self.cur.fetchall() return result wherein the output is a list of single element tuple (ex. I enter we all there ): [('tore',), ('ngaming',), ('sittam',)] (translate the input into

Multiply Adjacent Elements

◇◆丶佛笑我妖孽 提交于 2019-12-10 21:19:12
问题 I have a tuple of integers such as (1, 2, 3, 4, 5) and I want to produce the tuple (1*2, 2*3, 3*4, 4*5) by multiplying adjacent elements. Is it possible to do this with a one-liner? 回答1: Short and sweet. Remember that zip only runs as long as the shortest input. print tuple(x*y for x,y in zip(t,t[1:])) 回答2: >>> t = (1, 2, 3, 4, 5) >>> print tuple(t[i]*t[i+1] for i in range(len(t)-1)) (2, 6, 12, 20) Not the most pythonic of solutions though. 回答3: If t is your tuple: >>> tuple(t[x]*t[x+1] for x

How to split a CSV row so row[0] is the name and any remaining items are a tuple?

不羁岁月 提交于 2019-12-10 20:59:06
问题 I have a csv file in which the first column is the name of a baseball player, and then each subsequent item in the file is a statistic. I'd like to be able to import the file so that the player name was equal to a tuple of the statistics. Right now when I import the file using this code: Orioles = file("Orioles.csv", "rU") for row in Orioles: print row I get something like this: [Nick_Markakis, '.005', '.189', '.070', '.002', '.090'] [Adam_Jones, '.005', '.189', '.070', '.002', '.090'] I'd

How to use generic and Nullable<T> types in Dapper for materialization?

狂风中的少年 提交于 2019-12-10 20:51:41
问题 I have this call: public IObservable<T> Subscribe(object topic, string service, string connectionString, string query) { try { this.connection.ConnectionString = connectionString; this.connection.Open(); this.connection.Query<T>(query, new { transactionid = topic }).ToObservable().Subscribe(message => this.subject.OnNext(message)); return this.subject; } catch (Exception e) { this.subject.OnError(e); return this.subject; } finally { this.subject.OnCompleted(); this.connection.Close(); } }

Converting unordered list of tuples to pandas DataFrame

别来无恙 提交于 2019-12-10 20:48:38
问题 I am using the library usaddress to parse addresses from a set of files I have. I would like my final output to be a data frame where column names represent parts of the address (e.g. street, city, state) and rows represent each individual address I've extracted. For example: Suppose I have a list of addresses: addr = ['123 Pennsylvania Ave NW Washington DC 20008', '652 Polk St San Francisco, CA 94102', '3711 Travis St #800 Houston, TX 77002'] and I extract them using usaddress info =