tuples

Convert tuple to list in a dictionary

隐身守侯 提交于 2019-12-24 07:05:55
问题 I have a dictionary like this: a= {1982: [(1,2,3,4)], 1542: [(4,5,6,7), (4,6,5,7)]} and I want to change the all the tuples (1,2,3,4),(4,5,6,7),(4,6,5,7) to lists, in this case: [1,2,3,4], [4,5,6,7], [4,6,5,7] I have tried for key, value in a.items(): for i in value: i = tuple(i) but it does now work. How can I achieve it? 回答1: As far as I understand you want to convert each tuple in a list. You can do this using a dictionary comprehension: {k: [list(ti) for ti in v] for k, v in a.items()}

Haskell Esqueleto project to list of records instead of tuples

家住魔仙堡 提交于 2019-12-24 07:05:20
问题 In all the examples I have seen the results from esqueleto are projected into a list of tuples. This makes coding and maintenance harder because of lack of labels. For example: previousLogItems <- select $ from $ \li -> do orderBy [desc (li ^. LogItemId)] limit 10 return (li ^. LogItemId, li ^. LogItemTitle) Is there any way to get esqueleto to project the results to a list of records instead? 回答1: In fact you construct the tuple yourself. Indeed: previousLogItems <- select $ from $ \li -> do

Convert tuple to list in a dictionary

青春壹個敷衍的年華 提交于 2019-12-24 07:05:12
问题 I have a dictionary like this: a= {1982: [(1,2,3,4)], 1542: [(4,5,6,7), (4,6,5,7)]} and I want to change the all the tuples (1,2,3,4),(4,5,6,7),(4,6,5,7) to lists, in this case: [1,2,3,4], [4,5,6,7], [4,6,5,7] I have tried for key, value in a.items(): for i in value: i = tuple(i) but it does now work. How can I achieve it? 回答1: As far as I understand you want to convert each tuple in a list. You can do this using a dictionary comprehension: {k: [list(ti) for ti in v] for k, v in a.items()}

Create new tokens and tuples from existing ones based on conditions

谁都会走 提交于 2019-12-24 06:28:13
问题 This is very related to a previous question but I am having difficulties adapting for my use case. I have a sentence: "Forbes Asia 200 Best Under 500 Billion 2011" I have tokens like: oldTokens = [u'Forbes', u'Asia', u'200', u'Best', u'Under', u'500', u'Billion', u'2011'] And the indices of where a previous parser has figured out where there should be location or number slots: numberTokenIDs = {(7,): 2011.0, (2,): 200.0, (5,6): 500000000000.00} locationTokenIDs = {(0, 1): u'Forbes Asia'} The

Create new tokens and tuples from existing ones based on conditions

倖福魔咒の 提交于 2019-12-24 06:27:10
问题 This is very related to a previous question but I am having difficulties adapting for my use case. I have a sentence: "Forbes Asia 200 Best Under 500 Billion 2011" I have tokens like: oldTokens = [u'Forbes', u'Asia', u'200', u'Best', u'Under', u'500', u'Billion', u'2011'] And the indices of where a previous parser has figured out where there should be location or number slots: numberTokenIDs = {(7,): 2011.0, (2,): 200.0, (5,6): 500000000000.00} locationTokenIDs = {(0, 1): u'Forbes Asia'} The

counting non-empty lines and sum of lengths of those lines in python

情到浓时终转凉″ 提交于 2019-12-24 05:18:11
问题 Am trying to create a function that takes a filename and it returns a 2-tuple with the number of the non-empty lines in that program, and the sum of the lengths of all those lines. Here is my current program: def code_metric(file): with open(file, 'r') as f: lines = len(list(filter(lambda x: x.strip(), f))) num_chars = sum(map(lambda l: len(re.sub('\s', '', l)), f)) return(lines, num_chars) The result I get is get if I do: if __name__=="__main__": print(code_metric('cmtest.py')) is (3, 0)

Combine tuples in a list which have the same value [duplicate]

被刻印的时光 ゝ 提交于 2019-12-24 04:51:42
问题 This question already has answers here : Union find implementation using Python (4 answers) Find the intersection between sublists (2 answers) Finding common elements in list in python (4 answers) Closed 4 years ago . I have a list with tuples like this: L ={(1,2), (1,4), (1,3), (2,3), (3,4), (3,5), (4,5), (6,7)} I try to combine these to get equivalence classes (tuples of the same value are merged, like (1,2) and (2,3) becomes (1,2,3)). So you get: EQ = {(1,2,3,4,5), (6,7)} What's the

Filling a std::tuple

馋奶兔 提交于 2019-12-24 04:01:11
问题 I have a overloaded function which looks like: template<typename T> T getColumn(size_t i); template<> std::string getColumn<std::string>(size_t i) { if(i == 0) return "first"; else return "other"; } template<> int getColumn<int>(size_t i) { return i*10; } // ... Now I want to implement the function template<typename... Values> std::tuple<Values...> getColumns(); Which creates a tuple (for the return value) and calls getColumn for every element of the tuple (saving the return value in that

TypeScript union of strings not assignable to union of tuples in function

冷暖自知 提交于 2019-12-24 03:33:05
问题 The behaviour of these two examples should be identical, yet the second one errors. Why? // Example 1: const a: 'x' | 'y' = 'x'; const b: ['x'] | ['y'] = [a]; // ok // Example 2: function fn(a: 'x' | 'y') { const b: ['x'] | ['y'] = [a]; // ^ // Type '["x" | "y"]' is not assignable to type '["x"] | ["y"]'. // Type '["x" | "y"]' is not assignable to type '["x"]'. // Type '"x" | "y"' is not assignable to type '"x"'. // Type '"y"' is not assignable to type '"x"'. } You can try it on the

Write list of tuples to txt file

时间秒杀一切 提交于 2019-12-24 03:13:47
问题 Consider this list of tuples: list=[((0.0, 0.0), (0.00249999994412065, -509.707885742188), (0.00499999988824129, -1017.52648925781), (0.0087500000372529, -1778.51281738281), (0.0143750002607703, -2918.21899414063), (0.0228125005960464, -4609.91650390625))] I'd like to write the information to a txt file in this format: 0.0 0.0 0.00249999994412065 -509.707885742188 .... I've been using this code: with open(fname, 'w') as graphd: for row in list: print >>graphd, ', '.join(map(str, row)) graphd