python中如何解套列表提取元组的值

有一个列表a,
a = ["[('Eggs', 6), ('Coffee', 1), ('Pancake', 10), ('Cereal', 5)]",
"[('Coffee', 4), ('Pancake', 9), ('Cereal', 6), ('Eggs', 8)]",
"[('Eggs', 1), ('Coffee', 5), ('Cereal', 7), ('Pancake', 7)]",
"[('Pancake', 3), ('Cereal', 5)]",
"[('Eggs', 3), ('Pancake', 4)]",
"[('Pancake', 1), ('Cereal', 5)]"]

现在想解套列表提取每一个小列表里元祖的第一个值,请问该如何实现呢

代码如下,如果有帮助的话,请点击一下采纳谢谢:

a = ["[('Eggs', 6), ('Coffee', 1), ('Pancake', 10), ('Cereal', 5)]",
"[('Coffee', 4), ('Pancake', 9), ('Cereal', 6), ('Eggs', 8)]",
"[('Eggs', 1), ('Coffee', 5), ('Cereal', 7), ('Pancake', 7)]",
"[('Pancake', 3), ('Cereal', 5)]",
"[('Eggs', 3), ('Pancake', 4)]",
"[('Pancake', 1), ('Cereal', 5)]"]

for temp in a:
    temp = eval(temp)
    for x in temp:
        print(x[0])
a = ["[('Eggs', 6), ('Coffee', 1), ('Pancake', 10), ('Cereal', 5)]",
"[('Coffee', 4), ('Pancake', 9), ('Cereal', 6), ('Eggs', 8)]",
"[('Eggs', 1), ('Coffee', 5), ('Cereal', 7), ('Pancake', 7)]",
"[('Pancake', 3), ('Cereal', 5)]",
"[('Eggs', 3), ('Pancake', 4)]",
"[('Pancake', 1), ('Cereal', 5)]"]
b=[y[0] for y in [eval(x) for x in a]]
print(b)

取第一个的第一位,0 0

我的变量points中存储了两个co-ordinates:[(100, 50)]我试图用pyautogui.moveTo(points)移动我的鼠标,我得到了错误:

pyautogui.PyAutoGUIException: The supplied sequence must have exactly 2 or exactly 4 elements (0 were received).
我假设这意味着我传递的是一个列表对象,而不是坐标。

表达式[(100, 50)]是什么意思?我如何将x和y转换成两个元素。

我从中获取points的源代码:

import cv2 as cv
import numpy as np

class Vision:

properties

needle_img = None
needle_w = 0
needle_h = 0
method = None

constructor

def init(self, needle_img_path, method=cv.TM_CCOEFF_NORMED):
self.needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)

# Save the dimensions of the needle image
self.needle_w = self.needle_img.shape[1]
self.needle_h = self.needle_img.shape[0]

self.method = method

def find(self, haystack_img, threshold=0.5, debug_mode=None):
# run the OpenCV algorithm
result = cv.matchTemplate(haystack_img, self.needle_img, self.method)
# Get the all the positions from the match result that exceed our threshold
locations = np.where(result >= threshold)
locations = list(zip(*locations[::-1]))

for loc in locations:
    rect = [int(loc[0]), int(loc[1]), self.needle_w, self.needle_h]
    # Add every box to the list twice in order to retain single (non-overlapping) boxes
    rectangles.append(rect)
    rectangles.append(rect)
# Apply group rectangles
rectangles, weights = cv.groupRectangles(rectangles, groupThreshold=1, eps=0.5)

points = []
if len(rectangles):

    line_color = (0, 255, 0)
    line_type = cv.LINE_4
    marker_color = (255, 0, 255)
    marker_type = cv.MARKER_CROSS

    # Loop over all the rectangles
    for (x, y, w, h) in rectangles:

        # Determine the center position
        center_x = x + int(w/2)
        center_y = y + int(h/2)
        # Save the points
        points.append((center_x, center_y))

        if debug_mode == 'rectangles':
            # Determine the box position
            top_left = (x, y)
            bottom_right = (x + w, y + h)
            # Draw the box
            cv.rectangle(haystack_img, top_left, bottom_right, color=line_color, 
                        lineType=line_type, thickness=2)
        elif debug_mode == 'points':
            # Draw the center point
            cv.drawMarker(haystack_img, (center_x, center_y), 
                        color=marker_color, markerType=marker_type, 
                        markerSize=40, thickness=2)

if debug_mode:
    cv.imshow('Matches', haystack_img)

return points

"解套列表提取每一个小列表里元祖的第一个值",这句话理解上可能存在歧义。题主是要提取每个子列表的各个元组的第一个元素吗?如果是的话,下面这个代码可以参考。若是想要一维的结构,可将append方法改为extend方法。

a = ["[('Eggs', 6), ('Coffee', 1), ('Pancake', 10), ('Cereal', 5)]",
"[('Coffee', 4), ('Pancake', 9), ('Cereal', 6), ('Eggs', 8)]",
"[('Eggs', 1), ('Coffee', 5), ('Cereal', 7), ('Pancake', 7)]",
"[('Pancake', 3), ('Cereal', 5)]",
"[('Eggs', 3), ('Pancake', 4)]",
"[('Pancake', 1), ('Cereal', 5)]"]
result = list()
for item in [eval(piece) for piece in a]:
    result.append(list(map(lambda x:x[0], item)))

    
print(result)
[['Eggs', 'Coffee', 'Pancake', 'Cereal'], ['Coffee', 'Pancake', 'Cereal', 'Eggs'], ['Eggs', 'Coffee', 'Cereal', 'Pancake'], ['Pancake', 'Cereal'], ['Eggs', 'Pancake'], ['Pancake', 'Cereal']]
x=[]
for i in a:
    for j in eval(i):
        x.append(j)
        break
print(x)

img

print([y[0] for y in [eval(x) for x in a]])