简体   繁体   English

如何将字符串列表中的元素添加到python中的数组

[英]How to add elements in a list of strings to an array in python

I have a list of strings that look like this, and I need to add the first, second, and third entry of each to a integer list ab and c accordingly 我有一个看起来像这样的字符串列表,我需要将每个字符串的第一,第二和第三项添加到整数列表ab和c中

print cuts gives me 剪裁给我

[['3', '5', '10'], ['2', '8', '15'], ['7', '9', '25'], ['4', '6', '20'], ['9', '12', '50'], ['5', '7', '22'], ['3', '8', '17'], ['6', '9', '24'], ['8', '11', '40'], ['7', '10', '30'], []]

such that a = [3,2,7, ... , 7] b = [5, 8, 9, ... , 10] c = [10, 15, ... , 30] 这样a = [3,2,7,...,7] b = [5,8,9,...,10] c = [10,15,...,30]

(Also want these to be ints not strings) (也希望这些是整数而不是字符串)

I tried to delimit by 我试图用

cuts[i] = cuts[i].strip(",")

thinking that it would give me [[3] [5] [10] ] which would let me add with a for loop but python told me lists don't have a strip attribute 认为这会给我[[3] [5] [10]],这会让我添加一个for循环,但是python告诉我列表没有strip属性

Here are list comprehensions in basic form: 以下是基本形式的列表理解:

data = [['3', '5', '10'], ['2', '8', '15'], ['7', '9', '25'], ['4', '6', '20'],
        ['9', '12', '50'], ['5', '7', '22'], ['3', '8', '17'], ['6', '9', '24'],
        ['8', '11', '40'], ['7', '10', '30'], []]

a = [int(x[0]) for x in data if len(x) >= 1]
b = [int(x[1]) for x in data if len(x) >= 2]
c = [int(x[2]) for x in data if len(x) >= 3]

print a
print b
print c

Or, to keep them in a 2-dimensional list: 或者,将它们保留在二维列表中:

abc = [[int(x[i]) for x in data if len(x) >= i+1] for i in range(len(data[0]))]
print abc

Output: 输出:

[[3, 2, 7, 4, 9, 5, 3, 6, 8, 7], 
 [5, 8, 9, 6, 12, 7, 8, 9, 11, 10], 
 [10, 15, 25, 20, 50, 22, 17, 24, 40, 30]]

This can be done using the built in zip function. 可以使用内置的zip功能完成此操作。

a, b, c = zip(*cuts)

You can read about zip here . 您可以在此处阅读有关zip的信息 The gist is here - 要点在这里-

this function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables 此函数返回一个元组列表,其中第i个元组包含每个参数序列或可迭代对象中的第i个元素

If you then want your lists to hold numbers instead of strings, you can map them over the int function. 然后,如果您希望列表保留数字而不是字符串,则可以将它们映射到int函数上。

a = map(int, a)
b = map(int, b)
c = map(int, c)

I'm assuming that the empty list at the end of cuts is a typo -- otherwise your requirements don't quite make sense since there isn't a first, second and third element of an empty list. 我假设cuts结束时的空列表是一个错字-否则您的要求就没有意义了,因为空列表中没有第一,第二和第三元素。 You could always use 您可以随时使用

cuts = [cut for cut in cuts if len(cut) > 0]

if you want to cut cuts down to size. 如果您想缩小尺寸。

In any event, once that empty list is removed, what you are asking for is a sort of zip and can be done like thus: 无论如何,一旦删除了该空列表,您所需要的就是一种zip ,可以这样完成:

>>> cuts = [['3', '5', '10'], ['2', '8', '15'], ['7', '9', '25'], ['4', '6', '20'], ['9', '12', '50'], ['5', '7', '22'], ['3', '8', '17'], ['6', '9', '24'], ['8', '11', '40'], ['7', '10', '30']]
>>> a,b,c = ([int(s) for s in strings] for strings in zip(*cuts))
>>> a
[3, 2, 7, 4, 9, 5, 3, 6, 8, 7]
>>> b
[5, 8, 9, 6, 12, 7, 8, 9, 11, 10]
>>> c
[10, 15, 25, 20, 50, 22, 17, 24, 40, 30]

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

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