将hh:mm:ss转换为秒

时间:2022-11-13 02:44:28

I have two arrays of data. Both arrays contain dates in HH:MM:SS format. What I am trying to do is find the index for matching dates for both arrays. The issue I have is the ':' character is causing it to not work. This is my code...

我有两个数据阵列。两个数组都包含HH:MM:SS格式的日期。我想要做的是找到两个数组匹配日期的索引。我遇到的问题是':'字符导致它不起作用。这是我的代码......

import numpy as np

t1 = np.array(['11:13:10', '10:13:12', '10:13:40'])

t2 = np.array(['11:14:10', '10:14:12', '10:13:40'])

t_1 = np.in1d(t1,t2)

t_1_index = np.where(t_1 == True)

print t_1_index

The result of my code is printed as (array([], dtype=int64),) which is blank. How can I get it to return (array([0,0,1], dtype=int64),)? I know this question has been asked before and I have read the answers but it hasn't helped me solve my issue. Any help would be appreciated.

我的代码的结果打印为(array([],dtype = int64),),这是空白的。我怎样才能让它返回(数组([0,0,1],dtype = int64),)?我知道之前已经问过这个问题而且我已经阅读了答案,但这并没有帮助我解决我的问题。任何帮助,将不胜感激。

5 个解决方案

#1


1  

np.in1d sees if the element in array 1 is in array 2. So you're really doing 3X3 = 9 operations every time using that method instead of 3.

np.in1d查看数组1中的元素是否在数组2中。因此,每次使用该方法而不是3时,您实际上正在执行3X3 = 9次操作。

Try this.

t1 = ['11:13:10', '10:13:12', '10:13:40']
t2 = ['11:14:10', '10:14:12', '10:13:40']
t3 = []

for i in range(0, len(t1)):
  if t1[i] == t2[i]:
    t3.append(1)
  else:
    t3.append(0)

print(t3)

#2


0  

I think the problem is you mistaken the usage of np.where, which return the index of true values.

我认为问题是你错误地使用了np.where,它返回了真值的索引。

t_1 = np.in1d(t1,t2)
t_1_index = t_1 * 1

This should work~

这应该工作〜

#3


0  

t_1 = np.in1d(t1,t2)
##output = array([False, False,  True], dtype=bool)

##then you can do:
t_1.astype('int')  #this transforms booleans to integers
##output-> array([0, 0, 1])

#4


0  

In my actual code I had two numpy arrays of different length. So I adapted DiderDrogba344's method to accommodate that. My final code was as follows. Thanks everyone for your help.

在我的实际代码中,我有两个不同长度的numpy数组。所以我采用了DiderDrogba344的方法来适应这种情况。我的最终代码如下。谢谢大家的帮助。

import numpy as np

t1 = np.array(['11:13:10', '10:13:12', '10:13:40'])
t2 = np.array(['11:14:10', '10:14:12', '10:13:40', '10:14:16'])
t3 = []

t1 = np.ndarray.tolist(t1)
t2 = np.ndarray.tolist(t2)



for i in range(0, len(t1)):
    for j in range(0, len(t2)):
        if t1[i] == t2[j]:
            t3.append(1)
    else:
        t3.append(0)

print(t3)

if len(t1) < len(t2):
    index_t2 = t3.index(1)
    number_in_t1 = t2[index_t2]
    index_t1 = t1.index(number_in_t1)


else:
    index_t1 = np.where(t3 == 1)
    number_in_t2 = t1[index_t1]
    index_t2 = np.where(t2 == number_in_t2)

print index_t1
print index_t2

#5


0  

try this

map(lambda x: 1 if x else 0, [False, False, True])
[0, 0, 1]

or

map(lambda x,y: 1 if x==y else 0, ['11:13:10', '10:13:12', '10:13:40'],
                                  ['11:14:10', '10:14:12', '10:13:40'])
[0, 0, 1]

and

[(lambda x,y: 1 if x==y else 0)(*a) for a in zip(['11:13:10', '10:13:12', '10:13:40'], 
                                                 ['11:14:10', '10:14:12', '10:13:40'])]
[0, 0, 1]

#1


1  

np.in1d sees if the element in array 1 is in array 2. So you're really doing 3X3 = 9 operations every time using that method instead of 3.

np.in1d查看数组1中的元素是否在数组2中。因此,每次使用该方法而不是3时,您实际上正在执行3X3 = 9次操作。

Try this.

t1 = ['11:13:10', '10:13:12', '10:13:40']
t2 = ['11:14:10', '10:14:12', '10:13:40']
t3 = []

for i in range(0, len(t1)):
  if t1[i] == t2[i]:
    t3.append(1)
  else:
    t3.append(0)

print(t3)

#2


0  

I think the problem is you mistaken the usage of np.where, which return the index of true values.

我认为问题是你错误地使用了np.where,它返回了真值的索引。

t_1 = np.in1d(t1,t2)
t_1_index = t_1 * 1

This should work~

这应该工作〜

#3


0  

t_1 = np.in1d(t1,t2)
##output = array([False, False,  True], dtype=bool)

##then you can do:
t_1.astype('int')  #this transforms booleans to integers
##output-> array([0, 0, 1])

#4


0  

In my actual code I had two numpy arrays of different length. So I adapted DiderDrogba344's method to accommodate that. My final code was as follows. Thanks everyone for your help.

在我的实际代码中,我有两个不同长度的numpy数组。所以我采用了DiderDrogba344的方法来适应这种情况。我的最终代码如下。谢谢大家的帮助。

import numpy as np

t1 = np.array(['11:13:10', '10:13:12', '10:13:40'])
t2 = np.array(['11:14:10', '10:14:12', '10:13:40', '10:14:16'])
t3 = []

t1 = np.ndarray.tolist(t1)
t2 = np.ndarray.tolist(t2)



for i in range(0, len(t1)):
    for j in range(0, len(t2)):
        if t1[i] == t2[j]:
            t3.append(1)
    else:
        t3.append(0)

print(t3)

if len(t1) < len(t2):
    index_t2 = t3.index(1)
    number_in_t1 = t2[index_t2]
    index_t1 = t1.index(number_in_t1)


else:
    index_t1 = np.where(t3 == 1)
    number_in_t2 = t1[index_t1]
    index_t2 = np.where(t2 == number_in_t2)

print index_t1
print index_t2

#5


0  

try this

map(lambda x: 1 if x else 0, [False, False, True])
[0, 0, 1]

or

map(lambda x,y: 1 if x==y else 0, ['11:13:10', '10:13:12', '10:13:40'],
                                  ['11:14:10', '10:14:12', '10:13:40'])
[0, 0, 1]

and

[(lambda x,y: 1 if x==y else 0)(*a) for a in zip(['11:13:10', '10:13:12', '10:13:40'], 
                                                 ['11:14:10', '10:14:12', '10:13:40'])]
[0, 0, 1]