我想从名为a的字符串列表中创建一个整数列表。我要创建的列表显示在名为b的整数列表中。
a = ["ab","ac","ad","ae","af","ab","ab"]
b = [1,2,3,4,5,1,1]
我已经尝试过该解决方案,但要花费大量时间才能处理数千个数据。
a = ["ab","ac","ad","ae","af","ab","ab"]
b = list(set(a))
for i in range(0,len(a)):
if a[i] in b:
a[i] = b.index(a[i])+1
print(a)
谢谢
# Input array of strings.
a = ["ab","ac","ad","ae","af","ab","ab"]
d = {}
counter = 0
b = []
for item in a:
if item not in d:
counter += 1
d[item] = counter
b.append(d[item])
>>> b
[1, 2, 3, 4, 5, 1, 1]
a = ["ab","ac","ad","ae","af","ab","ab"]
from itertools import count
counter = count(1)
a_dict = dict()
b = []
for elem in a:
a_dict[elem] = a_dict.get(elem, next(counter))
b.append(a_dict[elem])
print(b)
输出:[1、2、3、4、5、1、1]
只需跟踪已看到的对象及其各自的编号。如果没有看到,它将在柜台上抓住下一个号码。
仅基于问题中提供的值,您可以将唯一值收集到列表中,然后使用唯一列表的索引为每个值提供数字以获得预期的输出。
a = ["ab", "ac", "ad", "ae", "af", "ab", "ab"]
unique_list = []
b = []
for value in a:
if value not in unique_list:
unique_list.append(value)
for value in a:
for ndex, unique_value in enumerate(unique_list):
if value == unique_value:
b.append(ndex+1)
break
print(b)
结果:
[1, 2, 3, 4, 5, 1, 1]
那表示在没有更多上下文的情况下,无法确定您的字符串值将转换为什么。
更新:
[基于注释中的问题,我还用一个集对此进行了测试,并针对set()
运行了10次,对list
运行了10次,新建了一个40,000个随机值集。除非我在这里做错了,否则列表似乎运行得更快。
import time
import random
times_for_set = []
times_for_list = []
times_for_dict = []
def run_comparison_set():
a = []
for _ in range(40000):
x = random.choice('abcdefghijklmnopqrstuvwxyz')
y = random.choice('abcdefghijklmnopqrstuvwxyz')
a.append('{}{}'.format(x, y))
unique_list = set(a)
b = []
start_time = time.time()
for value in a:
for ndex, unique_value in enumerate(unique_list):
if value == unique_value:
b.append(ndex+1)
break
times_for_set.append(time.time() - start_time)
def run_comparison_list():
a = []
for _ in range(40000):
x = random.choice('abcdefghijklmnopqrstuvwxyz')
y = random.choice('abcdefghijklmnopqrstuvwxyz')
a.append('{}{}'.format(x, y))
unique_list = []
b = []
for value in a:
if value not in unique_list:
unique_list.append(value)
start_time = time.time()
for value in a:
for ndex, unique_value in enumerate(unique_list):
if value == unique_value:
b.append(ndex + 1)
break
times_for_list.append(time.time() - start_time)
def run_comparison_dict():
a = []
for _ in range(40000):
x = random.choice('abcdefghijklmnopqrstuvwxyz')
y = random.choice('abcdefghijklmnopqrstuvwxyz')
a.append('{}{}'.format(x, y))
counter = 0
d = {}
b = []
start_time = time.time()
for item in a:
if item not in d:
counter += 1
d[item] = counter
b.append(d[item])
times_for_dict.append(time.time() - start_time)
for i in range(10):
run_comparison_set()
run_comparison_list()
run_comparison_dict()
print('Average time for set: ', sum(times_for_set) / len(times_for_set))
print('Average time for list: ', sum(times_for_list) / len(times_for_list))
print('Average time for dict: ', sum(times_for_dict) / len(times_for_dict))
结果:
Average time for set: 0.8128192901611329
Average time for list: 0.6368690490722656
Average time for dict: 0.00530548095703125
所以看来列表和集合比字典慢得多。