简体   繁体   English

更改python中的字符串列表

[英]changing a list of strings in python

I am trying to change this list我正在尝试更改此列表

['AAAAA   4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5',
 'BBB     5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2',
 'K       4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']

to something that looks like this看起来像这样的东西

AAAAA -- [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB -- [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
K -- [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]

i have tried multiple ways and cant figure it out我尝试了多种方法,但无法弄清楚

You just need to split the elements in each string, take the first element and set it as key of the dictionary, and convert the rest of the elements to integers, and store as values:您只需要拆分每个字符串中的元素,取第一个元素并将其设置为字典的键,并将其余元素转换为整数,并存储为值:

list_ = ['AAAAA   4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5',
 'BBB     5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2',
 'K       4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']
dict_ = {}
for string in list_:
    alpha, *numbers = string.split()
    dict_[alpha] = [*map(int,numbers)]

for alpha, numbers in dict_.items():
    print(f"{alpha} -- {numbers}")

Output:输出:

AAAAA -- [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB -- [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
K -- [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]

If you want to go fancy:如果你想花哨:

generator_ = (f"{alpha} -- {[*map(int,numbers)]}" for alpha, *numbers in [l.split() for l in list_])

print(*generator_, sep = '\n')

If you want to reproduce what you asked in the question:如果您想重现您在问题中提出的问题:

x = ['AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5', 'BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2', 'K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']

for elem in x:
  split = elem.split(" ")
  print("{} -- {}".format(split[0],[int(i) for i in split[1:]]))

This:这个:

  • Loops through the list x循环遍历列表x
  • Splits its items into a separate list split将其项目拆分为单独的列表split
  • Separates first element from rest with a "--" when printing打印时用“--”将第一个元素与其余元素分开

Or using a dictionary:或者使用字典:

x = ['AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5', 'BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2', 'K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']

# Create dictionary following above logic
d = dict()
for elem in x:
    split = elem.split(" ")
    d.update({split[0] : [int(i) for i in split[1:]]})

# Loop through its keys and values and print as needed
for k, v in d.items():
    print("{} -- {}".format(k, v))

Output:输出:

K -- [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]
AAAAA -- [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB -- [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
inputlist=['AAAAA   4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5', 'BBB     5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2', 'K       4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']
for item in inputlist:
  item_to_list=item.split(" ")
  temp_list=[int(i) for i in list(filter(None, item_to_list[2:]))]
  print("{0} -- {1}".format(item_to_list[0],str(temp_list)))

Output:输出:

AAAAA -- [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB -- [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3,2]
K -- [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]
x = ['AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5', 'BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2', 'K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']

for i in x:
    i = i.split(' ')
    tmp = {i[0]:[int(items) for items in i[1:]]}
    for i, j in tmp.items():
        print(f"{i} - {j}")

Output:输出:

AAAAA - [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB - [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
K - [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]

Try this:尝试这个:

name=["AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5",
      "BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2",
      "K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1"]


for item in name:
   thelist = list(map(int, ','.join(item.split(' ')[1:]).split(',')))    
   print(f"{item.split(' ')[0]} -- {thelist}")

output:输出:

AAAAA -- [4,2,1,2,4,2,4,4,5,2,2,1,5,2,4,3,1,1,3,3,5]
BBB -- [5,2,1,2,4,5,4,4,1,2,2,2,4,4,4,3,1,2,3,3,2]
K -- [4,1,2,1,2,1,2,5,1,1,1,1,4,2,2,1,5,1,3,4,1]

you can make a dict, and also use that dict to get your specific output if that's what you want:您可以创建一个 dict,也可以使用该 dict 来获取您想要的特定输出:

assuming your list is called full_list假设您的列表名为full_list

lists = [sub.split() for sub in full_list]
keys = [l[0] for l in lists]
vals = [list(map(int,l[1:])) for l in lists]
d = {k:v for k,v in zip(keys,vals)}

if desired to get that specific output:如果需要获得该特定输出:

for k,v in d.items():
    print(f'{k} -- {v}')

output:输出:

AAAAA -- [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB -- [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
K -- [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM