通过比较python中的现有列表创建列表并导出到excel

时间:2018-06-28 22:15:45

标签: python excel list for-loop venn-diagram

我正在处理python脚本,尝试通过以下方式创建带有列的excel文件:

a和b,但不在c

b和c,但不在a

c和a,但不在b

a,b和c。

输出看起来像3组维恩图,但我需要在列表中,然后我可以导出到excel电子表格。

const Sequelize = require('sequelize');

const sequelize = new Sequelize('mylibrary', 'isabellachen', 'projectorion', {
  host: 'localhost',
  dialect: 'postgres',
});

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });

const User = sequelize.define('author', {
  firstName: {
    type: Sequelize.STRING
  },
  lastName: {
    type: Sequelize.STRING
  }
});

const Book = sequelize.define('book', {
  title: {
    type: Sequelize.STRING
  },
  genre: {
    type: Sequelize.STRING
  }
});

User.Book = User.hasMany(Book, { foriegnKey: 'authorId' })
Book.User = Book.belongsTo(User, {
  as: 'author'
})

// User.sync({ force: true });
// Book.sync({ force: true });

Book.create({
  title: 'Pale Blue Dot',
  genre: 'non-fiction',
  author: { firstName: 'Carl', lastName: 'Sagan' }
}, {
    include: [{
      model: User,
      as: 'author'
    }]
  }
)

const createBook2 = async () => {
  const author = await User.findOne({
    where: {
      firstName: "Carl",
      lastName: "Sagan"
    },
  });

  Book.create({
    title: 'Pale Blue Dot',
    genre: 'non-fiction',
    authorId: author.id
  }, {
      include: [{
        model: User,
        as: 'author'
      }]
    }
  )
}




sequelize.sync();

1 个答案:

答案 0 :(得分:0)

您想要的是set,而不是列表。他们很好地支持这些操作。要进行设置,请在列表set(xs)上调用xs,或使用{'a', 'b', ... }之类的集合文字(与使用方括号的类似列表文字相反)。

有了这些设置,您可以做类似的事情

foo = {'a', 'b'}
bar = {'b', 'c', 'd'}

foo.difference(bar) # returns {'a'}
foo.union(bar) # returns {'a', 'b', 'c', 'd'}
foo.intersection(bar) # returns {'b'}
foo.symmetric_difference(bar) # returns {'a', 'c', 'd'}
...

您还可以通过调用list(foo)或类似方法将其转换回列表。使用这些工具,您可以轻松构建所需的维恩图。请注意,所有这些函数都返回 new 集,从不修改原始集。