簡體   English   中英

我可以打印以相同單詞開頭的所有值(從列中)嗎?

[英]Can I print all the values (from a column) that start with the same word?

這些是價值觀

我從數據框中的某個列中獲取了所有這些值,如您所見,第二個和第三個值以相同的單詞“Sicilian”開頭(我知道還有更多值)。 我想打印該列中以相同單詞開頭的所有值。 我在 python 中工作。

先感謝您。

你可以循環到每個值

我將創建 dict 來舉例

第一個循環到每個值並使用str.startswith函數

data = {"mark1":"tommy","Unkown":"data","mark2":"john","mark3":"Nick","not someone":None} # our data

marks = [] # the list that we going to append if the value_y starts with (x)

for mark in data:
    # returns False if the mark doesn't starts with "mark" else returns True
    if mark.startswith("mark"): # check if the mark starts with "mark"
        marks.append((mark,data[mark]))
print(marks)

你甚至可以在一行中完成

[marks.append((mark,data[mark])) if mark.startswith("mark") else None for mark in data]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM