Python face question

Source: Internet
Author: User

1. Remove duplicate elements from the list

#_*_coding:utf-8_*_ls= [1,3,6,9,2,5,1,3,7,8,6]Print('ls >>>', ls)#Method 1LS1 =list (set (LS))Print('LS1 >>>', LS1)#Method 2LS2 ={}.fromkeys (ls). Keys () LS2= [L forLinchLS2]Print('LS2 >>>', LS2)#Method 3LS3 =list (set (LS)) Ls3.sort (Key=ls.index)#maintain the order of the original list elementsPrint('LS3 >>>', LS3)#Method 4LS4 =[][ls4.append (i) forIinchLsifI not inchLS4]Print('LS4 >>>', LS4)

2. Delete the specified file

# _*_coding:utf-8_*_ Import  't.txt'os.remove (filepath)

3. Read the contents of the file and print it on the screen

With open ('1.txt','r') as fp:    =  Fp.readlines ()    print(contents)    for in  Contents:         Print (Content.replace ('\ n',') ")

5. The quickest way to get the list element backwards

ls = [1,3,6,9,2,5,1,3,7,8,6= ls[::-1]

4. Conversion of list and tuple to each other

TS = (1, 3, 6, 9, 2, 5, 1, 3, 7, 8, 6= [1, 3, 6, 9, 2, 5, 1, 3, 7, 8, 6]list (ts) tuple (LS)

Python face question

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.