I am having trouble with my file. I've split it so each line is a separate list eg.
myList = [['Adam','16','Yes'], ['Fred','22','No']]
and trying to make a variable that only select lines with contain a "Yes".
How would I go about doing this?
因此,您只想选择第三个成员等于"Yes"
:
selected = [data for data in your_list if data[2] == "Yes"]
myList = your_input_sequence
lambda function to select only matching text: lambda x: x[<position_of_string_to_compare>].lower()
Desired function:
result = filter(lambda x: x[2].lower() == 'yes', myList)
>>> myList
[['Adam', '16', 'Yes'], ['Fred', '22', 'No']]
>>> res = filter(lambda x: x[2].lower()=='yes', myList)
>>> res
[['Adam', '16', 'Yes']]
You can do this:
myList = [['Adam','16','Yes'], ['Fred','22','No']]
x='Yes'
list1=[]
for i in myList:
for j in i:
if x==j:
list1.append(i)
for x in list1:
a=" ".join(x)
f= open("textDocument.txt","w+")
f.write(a)
c=f.read()
f.close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.