简体   繁体   English

我有一个列表,其中每个元素都是列表中的单个数字。 如何提取数字?

[英]I have a list in which each element is a single number in a list. How do I extract the numbers?

I am a python beginner and I have a list.我是 python 初学者,我有一份清单。 It goes like this:它是这样的:

my_list=[
     {"first":"1"}, 
     {"second": "2"}, 
     {"third": "1"}, 
     {"four": "5"}, 
     {"five":"5"}, 
     {"six":"9"},
     {"seven":"7"}
    ]

I want to extract the single values of this list.我想提取此列表的单个值。 I wrote the following piece of code:我写了以下代码:

values = []

for element in my_list:
    val = list(element.values())
    values.append(val)

print(values)

This is my output:这是我的 output:

[['1'], ['2'], ['1'], ['5'], ['5'], ['9'], ['7']]

I want only the value numbers to be in my list.我只希望值数字出现在我的列表中。 [1,2,1,5,5,9,7] How do I fix my code? [1,2,1,5,5,9,7] 如何修复我的代码?

You are almost correct but list(element.values()) gives you a list, You don't want to append the list itself but the only item inside it.您几乎是正确的,但是list(element.values())给了您一个列表,您不想 append列表本身,而是其中的唯一项目。 So do a subscription:订阅也是如此:

values.append(val[0])

or you can use .extend() instead of .append() :或者您可以使用.extend()而不是.append()

values.extend(val)

If I were to write this, I would do one of these:如果我要写这篇文章,我会做以下其中一项:

print([list(d.values())[0] for d in my_list])
print([next(iter(d.values())) for d in my_list])

The .values() return a dict_values object, which is an iterable and since you're converting it to a list, so you can extract the first index in order to get your desired format like this .values()返回一个 dict_values object,这是一个可迭代的,因为你正在将它转换为一个列表,所以你可以提取第一个索引以获得你想要的格式,就像这样

values = []

for element in my_list:
    val = list(element.values())[0]
    values.append(val)

print(values)

You can do like this,你可以这样做,

In [1]: list(map(lambda x:list(x.values())[0], my_list))
Out[1]: ['1', '2', '1', '5', '5', '9', '7']
my_list=[
     {"first":"1"},
     {"second": "2"},
     {"third": "1"},
     {"four": "5"},
     {"five":"5"},
     {"six":"9"},
     {"seven":"7"}
     ]
values = [int(element.popitem()[1]) for element 
in my_list]
print(values)

When in doubt, simply pop the (most recently added) item:如有疑问,只需pop (最近添加的)项目:

>>> my_list = [
...     {"first": "1"},
...     {"second": "2"},
...     {"third": "1"},
...     {"four": "5"},
...     {"five": "5"},
...     {"six": "9"},
...     {"seven": "7"}
... ]
>>> [int(d.popitem()[1]) for d in my_list]
[1, 2, 1, 5, 5, 9, 7]

暂无
暂无

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

相关问题 我有一个从另一个列表创建的列表。 但是,我不希望两个名字彼此重复。 我怎样才能做到这一点? - I have a list which has been created from another list. However, I do not want two names repeating after each other. How can I do that? 如何在Python 2.7.2中获取一串数字并使每个数字成为列表中自己的元素? - How do I take a string of numbers in Python 2.7.2 and make each number its own element in a list? 在 Python 中,如何在列表中包含单个反斜杠元素? - In Python, how do I have a single backslash element in a list? 如何将列表中的每个元素乘以一个数字? - How do I multiply each element in a list by a number? Python函数需要一个元组,我所拥有的是一个列表。 我该如何调用此功能? - Python function expects a tuple, what I have is a list. How do I call this function? 我在列表中有多个数据框。 如何打印他们的名字? - I have multiple dataframes in a list. How to print their names? 如何找到列表的顺序,使其元素大于第二个列表中的相应元素。 我必须最大化这样的数字 - How to find an order of list such that its elements are greater than the corresponding elements in second list. I have to maximize such number 如何获取元素所在的列表? - How do I get the list in which an element is in? 如何在python中定义函数以选择列表。 - How do I define a function in python to choose a list. 我有一个列表,我希望列表的每个元素都作为一行 - I have a list where I want each element of the list to be in as a single row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM