tuples

iterate over list of tuple get error: unhashable type: 'slice' [closed]

坚强是说给别人听的谎言 提交于 2020-01-17 18:01:25
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 days ago . i have a list of tuple like this seq=[((), ('2',)), (('0', '1'), ('1', '4')), (('0', '2'), ('2', '0')), (('0', '2'), ('2', '4')),(('0', '4'), ('4', '0')), (('0', '4'), ('4', '2')), (('1', '2'), ('2', '4')), (('1', '4'), ('4', '1')), (('2',), ('2', '0')), (('2', '0'), ('0', '2')), (

Working with Lists and tuples

萝らか妹 提交于 2020-01-17 15:03:32
问题 My data looks like: X=[1,2,3,4] But I need it to look like: Y=[(1,2,3,4)] How does one do this in python? 回答1: Try this: l = [1,2,3,4] l2 = [tuple(l)] 回答2: To do this is simple >>> X = [1,2,3,4] >>> [tuple(X)] [(1, 2, 3, 4)] Convert X to a tuple and wrap it in a list. This is only one of probably many ways to do this. It doesn't seem like a very useful thing, so if you could explain why you want to do this, we may be able to suggest some more useful code for you. 来源: https://stackoverflow.com

How to implement a variadic tuple_map operation?

此生再无相见时 提交于 2020-01-17 13:09:24
问题 I want to implement a function which maps a variadic list of tuples to another tuple, given a function. It applies an N-ary function f to a list of elements taken from a list of N tuples (each with size at least M) and creates a new M-element tuple from the result of these applications. For a list of N tuples, each with M elements, the call to std::make_tuple would look like this pseudocode: std::make_tuple( f(t1_1, t2_1, t3_1, ..., tN_1), f(t1_2, t2_2, t3_2, ..., tN_2), f(t1_3, t2_3, t3_3, .

Why can tuples contain mutable items?

跟風遠走 提交于 2020-01-16 08:35:12
问题 If a tuple is immutable then why can it contain mutable items? It is seemingly a contradiction that when a mutable item such as a list does get modified, the tuple it belongs to maintains being immutable. 回答1: That's an excellent question. The key insight is that tuples have no way of knowing whether the objects inside them are mutable. The only thing that makes an object mutable is to have a method that alters its data. In general, there is no way to detect this. Another insight is that

Read lines of .txt or excel file into tuples

淺唱寂寞╮ 提交于 2020-01-16 01:17:18
问题 I would like to read line by line two .txt files. THE FILES HAVE DATA DIVIDED IN FIVE COLUMNS FILE_1: 843.19598 2396.10278 3579.13778 4210.15674 4209.37549 841.93976 2397.21948 3573.11963 4205.89209 4226.73926 842.01642 2397.72266 3573.06494 4202.88379 4226.93799 842.22083 2397.47974 3574.27515 4204.19043 4223.82088 842.42065 2397.20142 3575.47437 4205.52246 4220.64795 FILE_2: 3586.02124 2391.50342 837.45227 -837.29681 -2385.97513 3587.69238 2387.48218 836.60445 -840.75067 -2390.17529 3588

From pandas dataframe to tuples (for haversine module)

北城余情 提交于 2020-01-15 06:49:30
问题 I have a pandas dataframe my_df with the following columns : id lat1 lon1 lat2 lon2 1 45 0 41 3 2 40 1 42 4 3 42 2 37 1 Basically, I'd like to do the following : import haversine haversine.haversine((45, 0), (41, 3)) # just to show syntax of haversine() > 507.20410687342115 # what I'd like to do my_df["dist"] = haversine.haversine((my_df["lat1"], my_df["lon1"]),(my_df["lat2"], my_df["lon2"])) TypeError: cannot convert the series to < class 'float' > Using this, I tried the following : my_df[

Typescript: Create tuple from interface

心不动则不痛 提交于 2020-01-15 03:24:09
问题 Is it possible to generate a tuple type like [string, date, number] from an interface like {a: string, b: date, c: number} ? Scenario I'm trying to add typings to a function where you can either pass an object, or the values of the object's properties , in order. (Don't @ me, I didn't write the code.) // This is valid bookRepo.add({ title: 'WTF', authors: ['Herb Caudill', 'Ryan Cavanaugh'], date: new Date('2019-04-04'), pages: 123, }) // This is also valid bookRepo.add([ 'WTF', // title [

How do I remove element from a list of tuple if the 2nd item in each tuple is a duplicate?

萝らか妹 提交于 2020-01-14 19:18:07
问题 How do I remove element from a list of tuple if the 2nd item in each tuple is a duplicate? For example, I have a list sorted by 1st element that looks like this: alist = [(0.7897897,'this is a foo bar sentence'), (0.653234, 'this is a foo bar sentence'), (0.353234, 'this is a foo bar sentence'), (0.325345, 'this is not really a foo bar'), (0.323234, 'this is a foo bar sentence'),] The desired output leave the tuple with the highest 1st item, should be: alist = [(0.7897897,'this is a foo bar

Read tuples from text file

橙三吉。 提交于 2020-01-14 14:46:07
问题 I need to read tuples from a txt. I tried with numpy (using genfromtxt) but it didn't work (or at least, I don't know how). This is is my txt: (0,0) (0,0) (1,0) (2,3) (1,0) (1,1) (1,1) (3,3) (2,0) (1,2) (2,1) (4,4) (3,0) (2,2) (3,1) (5,5) I want to read one by one this columns and get a list of tuples: Trying with numpy I have this: import numpy as np File = np.genfromtxt('file.txt',delimiter=' ', dtype= tuple) But it returns a list of lists with bytes type elements. Obviously I can change

javascript — converting string to array of tuples

北慕城南 提交于 2020-01-14 06:53:06
问题 I have a string that I want to convert into array "[(6, 11), (12, 17), (18, 20)]" .split() wouldn't work (or at least I don't know how to separate the words) and JSON.parse always craps out with Uncaught SyntaxError: Unexpected token I'm converting like this: JSON.parse(THAT_GIVEN_LIST) Am I doing something wrong? How do I make this string into a nice list of [(6, 11), (12, 17), (18, 20)] 回答1: The parentheses that you are using are not syntactically correct for JSON. You pose that they mean