简体   繁体   English

如何比较第一个列表中的每个元素和第二个列表中的整个元素?

[英]How can I compare each element in first list with whole elements in the second list?

I'm very new to python, so I need your help . 我是python的新手,所以需要您的帮助。 I have csv file contains: 我的csv文件包含:

  Name           Movie_title            Comments
  Mark           toy story                love
  Sam            Grumpy Old Men           cool 
  Mark           Grumpy Old Men           Hate it
  Sam            toy story                didn't like it

I create a list contains only name without duplicate: names = ['Mark,Sam'] so what I want to create a text file with movie_title(eg, toy story.txt) contains all the comments in that movie 我创建了一个仅包含名称但没有重复的名称的列表:names = ['Mark,Sam'],所以我要创建带有movie_title的文本文件(例如,toy story.txt)包含该电影中的所有注释

toy story.txt 玩具总动员.txt

Mark -- toy story (love the movie) 马克-玩具总动员(爱电影)
Sam -- toy story (didn't like it) 山姆-玩具总动员(不喜欢)

Grumpy Old Men.txt 脾气暴躁的老男人.txt

Sam -- Grumpy Old Men (cool) 山姆-脾气暴躁的老男人(酷)
Mark -- Grumpy Old Men (Hate it) 马克-脾气暴躁的老人(讨厌它)

with open('data.csv', 'rt') as f:
 reader = csv.reader(f, delimiter=',')
  next(reader)# skip header
  for row in reader:
    for name in names:
       if name == row[0]:
         for i in row[0]:
          txt = open(os.path.join(dir_name, names + ".txt"),'w+')
          newlist.append(name+row[2])
          txt.write(newlist)

any idea !!!! 任何想法 !!!!

A bit simpler. 有点简单。 Because each line has the fields name, movie and comment (row[0], row[1] and row[2], respectively), you can just directly write everything from the fetched line: 因为每一行都有字段名称,电影和注释(分别为行[0],行[1]和行[2]),所以您可以直接从提取的行中编写所有内容:

#!/bin/python
import csv
with open('data.csv', 'rt') as f:
  reader = csv.reader(f, delimiter=',')
  next(reader)# skip header
  for row in reader:
    with open(row[1]+".txt","a") as mov:
      mov.write(row[0]+" -- "+row[1]+" ("+row[2]+")\n")

暂无
暂无

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

相关问题 只要列表中的项目相等,按现有顺序将列表的每个元素与第二个列表的元素进行比较 - Compare each element of a list in existing order with elements of a second list in order as long as items in lists are equal 如何替换PYTHON列表中每行的第一个元素? - How can I replace the first element of each line in a list in PYTHON? 给定一个元组列表,如果第一个元素相同,我如何在每个相似的元组中添加第二个元素 - Given a list of tuples, if the first element is the same, how do i add the second element inside each similar tuple 如何删除列表的第二个元素 - How can I delete second element of list 如何打印此列表中的第二个元素? - How can I print the second element in this list? 如何在列表列表中选择每个列表的前几个元素? - How do I select the first elements of each list in a list of lists? 如何使用python比较列表中的元素并检查第一个列表元素是否包含在另一个列表的元素中 - How to compare elements in lists and check if first list element contains in another list's element using python 如何制作一个新列表,该列表具有每个列表的第一个元素但分为 3 个? - How can I make a new list that has the first element of each of the list but split into 3? 比较列表中的每个元素 - Compare each of element in list 如何比较列表中的每个元素与另一个列表中的每个元素? - how to compare each element in a list with each element in another list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM