slice

Pandas: SettingWithCopyWarning, trying to understand how to write the code better, not just whether to ignore the warning

喜欢而已 提交于 2019-12-23 23:06:59
问题 I am trying to change all date values in a spreadsheet's Date column where the year is earlier than 1900, to today's date, so I have a slice. EDIT: previous lines of code: df=pd.read_excel(filename)#,usecols=['NAME','DATE','EMAIL'] #regex to remove weird characters df['DATE'] = df['DATE'].str.replace(r'[^a-zA-Z0-9\._/-]', '') df['DATE'] = pd.to_datetime(df['DATE']) sample row in dataframe: name, date, email [u'Public, Jane Q.\xa0' u'01/01/2016\xa0' u'jqpublic@email.com\xa0'] This line of code

Elegant way to convert a slice of one type to a slice of an equivalent type?

半城伤御伤魂 提交于 2019-12-23 21:42:39
问题 A motivating example: Implementing various scheduling "strategies", which sort a list of Jobs. type Job struct { weight int length int } // Given a slice of Jobs, re-order them. type Strategy func([]Job) []Job func Schedule(jobs []Job, strat Strategy) []Job { return strat(jobs) } One very simple strategy is to execute the shortest jobs first (disregarding their weight/priority). func MinCompletionTimes(job []Job) []Job { // Hmm... } Well, this strategy is nothing more than a sort on job

Combination of colon-operations in MATLAB

六月ゝ 毕业季﹏ 提交于 2019-12-23 19:25:55
问题 I have a question concerning the colon operator and expansion of vectors in MATLAB. My problem is to understand how the following line of code expands, to be able to use it for other sequences. The line of MATLAB code is: a(1:2:5) = 1:-4:-7 Note that a is not defined before the expansion. This returns the vector a = 1 0 3 0 -7 I know how the colon operator works with {start}:{step}:{stop} , my problem is to understand how and why the combination of a(1:2:5) and 1:-4:-7 returns a vector of

Slicing a 20×20 area around known indices (x, y) in a numpy array

ぐ巨炮叔叔 提交于 2019-12-23 17:39:13
问题 I have a large 2D numpy array for which I know a pair of indices which represent one element of the array. I want to set this element and the surrounding 20×20 area equal to zero; I have attempted using a slicing technique: s = array[x:10, y:10] s == 0 However, although x and y are previously defined, this is an 'invalid slice'. I would appreciate any suggestions as to how I can accomplish this as I am new to Python. 回答1: my_array[x - 10:x + 10, y - 10:y + 10] = 0 or s = my_array[x - 10:x +

Finding Unique Items in a Go Slice or Array

筅森魡賤 提交于 2019-12-23 12:24:33
问题 I'm pretty new to go and I'm really, really confused right now. Let's say I have a list of coordinates and lets say I have some doubles in this list of coordinates. I can't for the life of me figure out how to make a unique list. Normally in Python I can "cheat" with sets and other built-ins. Not so much in Go. package main import ( "fmt" "reflect" ) type visit struct { x, y int } func main() { var visited []visit var unique []visit visited = append(visited, visit{1, 100}) visited = append

Golang: Slicing and populating byte arrays

旧街凉风 提交于 2019-12-23 10:47:12
问题 I'm trying to write a packet protocol using golang. As the protocol will have a fixed length, it seems like a good starting point to allocate the exact amount of memory. E.g. packet := make([]byte, 1024) What I don't understand is how to then populate specific elements of that packet. I want to say something like:- slice = pointer(packet[512]) slice = []byte("abcdef") The result being that packet[512:518] == []byte("abcdef"). The docs I've read on Arrays and Slices show how to modify a single

Creating a sliding window iterator of slices of chars from a String

懵懂的女人 提交于 2019-12-23 09:44:02
问题 I am looking for the best way to go from String to Windows<T> using the windows function provided for slices. I understand how to use windows this way: fn main() { let tst = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; let mut windows = tst.windows(3); // prints ['a', 'b', 'c'] println!("{:?}", windows.next().unwrap()); // prints ['b', 'c', 'd'] println!("{:?}", windows.next().unwrap()); // etc... } But I am a bit lost when working this problem: fn main() { let tst = String::from("abcdefg"); let

Invert slice in python

醉酒当歌 提交于 2019-12-23 09:31:48
问题 Is there any simple way to invert a list slice in python? Give me everything except a slice? For example: Given the list a = [0,1,2,3,4,5,6,7,8,9] I want to be able to extract [7,8,9,0,1,2] i.e. everything but a[3:7]. Thinking about it logically, I thought that a[-3:3] would give me what I want, but it only returns an empty list. I am preferring a solution which will work for both python 2 and 3 回答1: If you're willing to destroy the list (or a copy of it) you can cut out the part you don't

Replacing selected elements in a list in Python

 ̄綄美尐妖づ 提交于 2019-12-23 08:58:11
问题 I have a list: mylist = [0, 0, 0, 0, 0] I only want to replace selected elements, say the first, second, and fourth by a common number, A = 100 . One way to do this: mylist[:2] = [A]*2 mylist[3] = A mylist [100, 100, 0, 100, 0] I am looking for a one-liner, or an easier method to do this. A more general and flexible answer is preferable. 回答1: Especially since you're replacing a sizable chunk of the list , I'd do this immutably: mylist = [100 if i in (0, 1, 3) else e for i, e in enumerate

How to slice middle element from list

不想你离开。 提交于 2019-12-23 08:49:11
问题 Rather simple question. Say I have a list like: a = [3, 4, 54, 8, 96, 2] Can I use slicing to leave out an element around the middle of the list to produce something like this? a[some_slicing] [3, 4, 8, 96, 2] were the element 54 was left out. I would've guessed this would do the trick: a[:2:] but the result is not what I expected: [3, 4] 回答1: You cannot emulate pop with a single slice, since a slice only gives you a single start and end index. You can, however, use two slices: >>> a = [3, 4,