두부찌개 파이썬( Tofusoup python)

블로그 이미지

tofusoup429

파이썬에 대한 정보를 공유하는 개인 블로그입니다.

'Regular Python'에 해당되는 글 3건

제목 날짜
  • 파이썬 format을 이용해 초간단하게 1000의 단위마다 콤마 붙이기 (Putting comma every 3 digit in python) 2017.12.06
  • secrets 모듈을 사용해 리스트에서 무작위 item 추출하는 방법 (Extracting a random member from a list) 2017.12.02
  • ast module for converting a List-type-string into a real python list 2017.05.07

파이썬 format을 이용해 초간단하게 1000의 단위마다 콤마 붙이기 (Putting comma every 3 digit in python)

Regular Python 2017. 12. 6. 14:48

숫자를 보기 좋게 만들기 위해서 천의 단위마다 콤마찍는 것이 좋다. 파이썬 스트링 format 메소드를 사용해 아주 간단하게 이게 가능하다.


Putting comma every 3 digit in a number can be done very easily by python's string format method. This is not only for leaving a record for myself in the future and sharing with those who do not know this)


print("{:,}".format(234234234234))


을 실행해보면,

On execution of the code above


'234,234,234,234'


이렇게 나온다. 물론 반환되는 값은 콤마를 포함하는 string이다. 따라서 계산이 다 끝난 최종값을 넣어주는 것이 좋겠다.

(you get that. Of course it returns a string. Thus, you better use this after every calculation has been done. )


이상

(End)



'Regular Python' 카테고리의 다른 글

secrets 모듈을 사용해 리스트에서 무작위 item 추출하는 방법 (Extracting a random member from a list)  (0) 2017.12.02
ast module for converting a List-type-string into a real python list  (0) 2017.05.07
Posted by tofusoup429

secrets 모듈을 사용해 리스트에서 무작위 item 추출하는 방법 (Extracting a random member from a list)

Regular Python 2017. 12. 2. 11:14

리스트에서 무작위로 배열안의 멤버를 추출하는 법을 소개한다.

(I introduce a simple way to extract one random member from a list. with python)


물론 random모듈로,

(Of course, you could do the same thing with random module.)

import random


alist= [1,3,4,5,3,2,1]

n = random.randint(0, len(alist)-1))

print(alist[n])


으로 해도 큰 문제는 없다마는 다음 세줄의 코드를 두줄로 줄일 수 있다.

(There is no problem doing it with random module as you see. But it requires 3 lines besides the import statement.)


python3.6.3에는 secrets이라는 모듈이 새로 생겼다. 훨씬 좋은 난수 발생기라고 한다.

(In python 3.6.3, the latest version as of this writing, there is a new module called secrets. According to the documentation, it produces much better randomness, which I still do not understand exactly.)


The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.


import secrets


alist= [1,3,4,5,3,2,1]

print(secrets.choice(alist))


이렇게 하면 좀 더 깔끔하고 쉬운 코드로 무작위 추출을 할 수 있다.

(You can extract one random member from a list only with 2 lines.) Good.



'Regular Python' 카테고리의 다른 글

파이썬 format을 이용해 초간단하게 1000의 단위마다 콤마 붙이기 (Putting comma every 3 digit in python)  (0) 2017.12.06
ast module for converting a List-type-string into a real python list  (0) 2017.05.07
Posted by tofusoup429

ast module for converting a List-type-string into a real python list

Regular Python 2017. 5. 7. 09:16

The 'literal_eval()' function in module ast, which stands for Abstract Syntax Trees, is pretty useful when you want to turn "a string-formatted-list" into "a real list".


For example, you have a String aString = "['a','b','c']" and you want aString to be a real list, ['a','b','c'] . That is when you want to use the the 'literal_eval()' in ast module. It's a internal module so you do not need to install it additionally.


Example of Usage

Let me show you simple examples of how to use it.

import ast

aString="[3,4,2,'efe',3]"
aList=ast.literal_eval(aString)

First I defined a list-formatted-string and I want to revert it to a real python list, aList.

print(aList)
>> [3, 4, 2, 'efe', 3]
print(alist[1])
>> 4

Boom. It works.


It also works when a list-format string contains dictionaries-format string such as bString="[{'1':'a'},{'2':'b'}]"

import ast

bString = "[{'1':'a'},{'2':'b'}]"
bList = ast.literal_eval(bString)
print(bList)
>> [{'1': 'a'}, {'2': 'b'}]
print(bList[1])
>> {'2': 'b'}
print(bList[1]['2'])
>> b

Boom. It also works.


The strings were converted into real lists and the dictionaries seamlessly.

Hope this one helps

'Regular Python' 카테고리의 다른 글

파이썬 format을 이용해 초간단하게 1000의 단위마다 콤마 붙이기 (Putting comma every 3 digit in python)  (0) 2017.12.06
secrets 모듈을 사용해 리스트에서 무작위 item 추출하는 방법 (Extracting a random member from a list)  (0) 2017.12.02
Posted by tofusoup429
이전페이지 다음페이지
블로그 이미지

파이썬에 대한 정보를 공유하는 개인 블로그입니다.

by tofusoup429

공지사항

    최근...

  • 포스트
  • 댓글
  • 트랙백
  • 더 보기

태그

  • AST
  • GUI
  • converting stringto list
  • Python
  • kivy
  • 파이썬
  • 키비

글 보관함

«   2025/07   »
일 월 화 수 목 금 토
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

링크

카테고리

Python (9)
Regular Python (3)
Django Python (1)
Configuration (2)
Kivy Project (0)
project1. TimerApp (0)
기타 (2)

카운터

Total
Today
Yesterday
방명록 : 관리자 : 글쓰기
tofusoup429's Blog is powered by daumkakao
Skin info material T Mark3 by 뭐하라
favicon

두부찌개 파이썬( Tofusoup python)

파이썬에 대한 정보를 공유하는 개인 블로그입니다.

  • 태그
  • 링크 추가
  • 방명록

관리자 메뉴

  • 관리자 모드
  • 글쓰기
  • Python (9)
    • Regular Python (3)
    • Django Python (1)
    • Configuration (2)
    • Kivy Project (0)
      • project1. TimerApp (0)
    • 기타 (2)

카테고리

PC화면 보기 티스토리 Daum

티스토리툴바