简体   繁体   English

将内部 0 和 1 索引附加到第二索引二维列表列表中的所有元素 - python

[英]Append inner 0 & 1st index to all elements in 2nd index two-dimensional List of Lists - python

Hello new to python here... wondering what the best way is to solve a problem like this.你好,这里是 python 的新手......想知道解决这样的问题的最佳方法是什么。

I have a 2d array that look something like this:我有一个看起来像这样的二维数组:

a = [['October 17', 'Manhattan', '10024, 10025, 10026'], 
     ['October 17', 'Queen', '11360, 11362, 11365, 11368']]

Would like to iterate over this to create a new list or data frame that looks like the following:想迭代这个以创建一个新的列表或数据框,如下所示:

10024, October 17, Manhattan
10025, October 17, Manhattan
10026, October 17, Manhattan
11360, October 17, Queens
11362, October 17, Queens
11365, October 17, Queens
11368, October 17, Queens

Any insight would be greatly appreciated.任何见解将不胜感激。

Thank you.谢谢你。

You may need to iterate over the values, and for each iterate over the several indices you have您可能需要迭代这些值,并且每次迭代您拥有的几个索引

values = [['October 17', 'Manhattan', '10024, 10025, 10026'],
          ['October 17', 'Queens', '11360, 11362, 11365, 11368']]

result = [[int(idx), row[0], row[1]]
          for row in values
          for idx in row[2].split(',')]
df = DataFrame(result, columns=['idx', 'date', 'place'])

To obtain获得

[[10024, 'October 17', 'Manhattan'], [10025, 'October 17', 'Manhattan'], 
 [10026, 'October 17', 'Manhattan'], [11360, 'October 17', 'Queens'], 
 [11362, 'October 17', 'Queens'], [11365, 'October 17', 'Queens'], 
 [11368, 'October 17', 'Queens']]


     idx        date      place
0  10024  October 17  Manhattan
1  10025  October 17  Manhattan
2  10026  October 17  Manhattan
3  11360  October 17     Queens
4  11362  October 17     Queens
5  11365  October 17     Queens
6  11368  October 17     Queens

Azro's answer is great, but here's a way to solve this problem that is more explicit (if you're truly new to Python, this approach might be more clear so you can understand what is happening — it doesn't rely on nested list comprehension.) Azro 的回答很好,但这里有一种更明确的方法来解决这个问题(如果你真的是 Python 新手,这种方法可能更清楚,所以你可以理解发生了什么——它不依赖于嵌套列表理解.)

a = [['October 17', 'Manhattan', '10024, 10025, 10026'], 
['October 17', 'Queen', '11360, 11362, 11365, 11368']]

# initialize an empty list to put our reformatted data into
final_result = []

for inner_list in a:
    # get the first two items
    first_item = inner_list[0]
    second_item = inner_list[1]

    # split the third list item, and remove trailing and leading whitespace
    remaining_data = [x.strip() for x in inner_list[2].split(',')]

    # iterate over the remaining data
    for item in remaining_data:
        # first, create a new sublist containing our first two items
        new_entry = [item, first_item, second_item]

        # add our new_entry into the final result list
        final_result.append(new_entry)

print(final_result)

暂无
暂无

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

相关问题 Python:如何遍历两个列表中的每个值,计算在第一个列表中值&lt;22或在第二个列表中&lt;27的出现次数? - Python: How to iterate through each value in two lists, count the occurrences that value is < 22 in the 1st list OR < 27 in the 2nd list? 循环遍历两个列表以查找第一个列表中的元素是否存在于第二个列表中 - loop through two lists to find if an element from 1st list exists in the 2nd list Python:如果列表中的第一个元素重复并且第二个元素在列表系列中最低,则删除列表中的列表 - Python: delete list in list if 1st element in list is duplicated and 2nd element is lowest in lists series 如何对列表列表进行排序并按间隔仅保留每个第一个元素的最大第二个元素? - How to sort a list of lists and and to keep only the maximal 2nd element of each of the 1st elements by intervals? 如何对列表列表进行排序并仅保留每个第一个元素的最大第二个元素? - How to sort a list of lists and and to keep only the maximal 2nd element of each of the 1st elements? 从列表列表中删除第一个元素,然后是第一个和第二个元素[保留] - Removing the 1st element then 1st and 2nd element from a list of lists [on hold] 给定两个二维点列表,如何为第一个列表中的每个点找到第二个列表中最近的点? - Given two lists of 2d points, how to find the closest point in the 2nd list for every point in the 1st list? Pandas MultiIndex:对每个第一个索引使用相同的第二个索引 - Pandas MultiIndex: Using same 2nd index for each 1st index 如何在python numpy的二维数组中将位置(第2、第3或第4等)转换为索引(第1位置为00,第2位置为01等)? - how can I convert position (2nd, 3rd or 4th etc ) to index(00 for 1st position, 01 for 2nd etc) in 2D array in python numpy? Python的二维列表 - Two-dimensional lists for Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM